HttpUtil工具类发送post请求
2017-08-17 10:55
423 查看
使用apache的HttpClient发送post请求。
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
private static String charset = "utf-8";
public static String postMethod(String url, String json) throws ClientProtocolException, IOException{
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
StringEntity entity = new StringEntity(json, charset);//解决中文乱码问题
entity.setContentEncoding(charset);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity, charset);
}
return null;
}
}
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
private static String charset = "utf-8";
public static String postMethod(String url, String json) throws ClientProtocolException, IOException{
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
StringEntity entity = new StringEntity(json, charset);//解决中文乱码问题
entity.setContentEncoding(charset);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity httpEntity = response.getEntity();
return EntityUtils.toString(httpEntity, charset);
}
return null;
}
}
相关文章推荐
- HttpClient发送get和post工具类
- Java工具类_模拟HTTP POST请求
- HttpClientUtil发送json格式请求
- java发送http的get、post请求
- java发送http的get、post请求
- Web Service系列之实例之使用http.client发送SOAP POST请求
- HttpClientUtil工具类,发送get请求和post请求
- Java中发送http的get、post请求
- 高德地图web端笔记;发送http请求的工具类
- java代码发送JSON格式的httpPOST请求
- java发送http的get、post请求
- Java HttpClient 发送multipart/form-data带有Json文件的Post请求
- java发送http的get、post请求
- HttpClientUtil请求http地址的工具类
- Android 服务器连接工具类HttpUtil
- 使用HttpURLConnection发送POST请求
- 使用HttpWebRequest发送POST请求
- java发送http的get、post请求
- 自己写的Android端HttpUtil工具类
- httpClient 用于发送HTTP请求的工具类