import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
/**
* httpclient使用get,post请求调用第三方webservice
*
* @author wuz 2017年9月26日下午2:35:14
*/
public class HttpClientDemo {
/**
* httpGet请求发送webservice
*
* @param url
* @return
* @throws IOException
*/
public static String httpGet(String url) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse httpResponse = null;
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectionRequestTimeout(2000)
.build();
httpGet.setConfig(requestConfig);
httpResponse = httpClient.execute(httpGet);
InputStream inputStream = null;
// System.out.println(+httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpResponseEntity = httpResponse.getEntity();
// System.out.println("响应内容:"+EntityUtils.toString(httpResponseEntity));//entity的流不可重复读,查看源码可以发现此处流已关闭
inputStream = httpResponseEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
// System.out.println("----------" + stringBuffer.toString()
// + "---------");
return String.valueOf(stringBuffer);
} else {
System.out.println("请求出错!");
}
return null;
}
/**
* httpPost请求发送webservice
*
* @param url
* @param nameValuePairs
* @return
* @throws IOException
*/
public static String httpPost(String url, List<NameValuePair> nameValuePairs) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse httpResponse = null;
HttpEntity httpResponseEntity = null;
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs, "utf-8");
httpPost.setEntity(urlEncodedFormEntity);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectionRequestTimeout(5000)
.build();
httpPost.setConfig(requestConfig);
httpResponse = httpClient.execute(httpPost);
InputStream inputStream = null;
// System.out.println(httpResponse.getStatusLine());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
httpResponseEntity = httpResponse.getEntity();
// System.out.println("响应内容:"+EntityUtils.toString(httpResponseEntity));//entity的流不可重复读,查看源码可以发现此处流已关闭
inputStream = httpResponseEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
// System.out.println("----------" + stringBuffer.toString()
// + "---------");
return String.valueOf(stringBuffer);
} else {
System.out.println("请求出错!");
}
httpResponse.close();
httpClient.close();
inputStream.close();
return null;
}
/**
* jdk自带的get请求
*
* @param action
* @return
* @author wuz
*/
public static String get(String action) {
String message = null;
try {
URL urlGet = new URL(action);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
message = new String(jsonBytes, "UTF-8");
} catch (Exception e) {
message = null;
}
return message;
}
/**
* post
*
* @param reqUrl
* @param requestXml
* @return
* @throws Exception
* @author wuz
*/
public static String post(String reqUrl, String requestXml) throws Exception {
// 创建url地址 例子:"http://127.0.0.1:8080/hello?wsdl"
URL url = new URL(reqUrl);
// 打开连接
URLConnection conn = url.openConnection();
// 转换成HttpURL
HttpURLConnection httpConn = (HttpURLConnection) conn;
// 打开输入输出的开关
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
// 设置请求方式
httpConn.setRequestMethod("POST");
// 设置请求的头信息
httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
// 拼接请求消息
String data = requestXml;
// 获得输出流
OutputStream out = httpConn.getOutputStream();
// 发送数据
out.write(data.getBytes());
// 判断请求成功
String message = null;
if (httpConn.getResponseCode() == 200) {
// 获得输入流
InputStream in = httpConn.getInputStream();
// 使用输入流的缓冲区
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String line = null;
// 读取输入流
while ((line = reader.readLine()) != null) {
sb.append(line);
}
message = sb.toString();
}
return message;
}
}
发送http-get,post请求工具类
最新推荐文章于 2024-07-15 21:25:43 发布