今天,小白的 get、post 请求。。。。。。
一、对于 get 请求,就是 url 和参数合并成一个接口请求地址
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class DoGetRequest {
/*
* get请求方式
* @author:Me
*/
public static String httpGetMethod(String url,Map<Object,Object> map) throws InstantiationException, IllegalAccessException {
HttpClient httpClient = new DefaultHttpClient();
Object value;
Object urlInfo = "";
for(Object key : map.keySet()){
value = map.get(key);
urlInfo += key+"="+value+"&";
}
urlInfo = urlInfo.toString().substring(0, urlInfo.toString().length()-1);
url = url + "?" + urlInfo.toString();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
StringBuilder sb = new StringBuilder();
InputStream inputStream = httpResponse.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String temp = "";
while((temp = br.readLine()) != null){
sb.append(temp + "\n");
}
return sb.toString();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
二、对于 post 请求,组装好参数类型,发送请求
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
@SuppressWarnings("deprecation")
public class DoPostMethod {
/*
* author:ME
*/
@SuppressWarnings("deprecation")
public String HttpClientPostMethod(String url,Map map) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
InputStream responseStream = null;
//参数形式为JSON格式
JSONObject parameter = new JSONObject(map);
try {
StringEntity entity = new StringEntity(parameter.toString());
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json"); //设置参数类型为JSON格式
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = httpResponse.getEntity();
responseStream = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
StringBuilder sb = new StringBuilder();
String temp = "";
while((temp = br.readLine()) != null){
String str = new String(temp.getBytes(), "UTF-8");
sb.append(str + "\n");
}
return sb.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}