post
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
// http请求
public static Result Post(String path, Map<String, String> param) {
String url = "http://192.168.1.115:9191" + path;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Host", "****");
headerMap.put("Accept-Encoding", "gzip");
headerMap.put("charset", "utf-8");
List<NameValuePair> pList = new ArrayList<>();
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
pList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
for (Map.Entry<String, String> entry : param.entrySet()) {
pList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pList, "UTF-8");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
String content = "";
if (resEntity != null) {
content = EntityUtils.toString(resEntity, "UTF-8");
}
Result res = new Gson().fromJson(content, Result.class);
return res;
} catch (Exception e) {
String msg = "请求数说读者的GO服务失败, url: " + url;
LogDefine.LOG.error(msg, e);
return new Result<String>(null, false, msg, null);
}
}