一、初识HttpClient
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
二、HttpClient使用
使用之前,需要导入maven坐标,不使用maven就要自己取下jar包,apache官网就有。
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
使用下列工具类即可完成HTTP的请求
public class HttpClientUtil{
/**
* 发送TFS get请求
*
* @param url
* @param apiAuthString
* @return
*/
public static String sendGet(String url, String apiAuthString) {
String result = null;
try {
URL url1 = new URL(url);
//base加密
byte[] authEncBytes = Base64.encodeBase64(apiAuthString.getBytes());
String authStringEnc = new String(authEncBytes);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
result = line;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 发送TFS post请求
*
* @param data 参数
* @param requestUrl 请求地址
* @param apiAuthString 授权书
* @return 返回结果
* @throws IOException
* @author
*/
public static String sendPost(JSONObject data, String requestUrl, String apiAuthString)
throws IOException {
HttpClient httpClient = new HttpClient();// 客户端实例化
PostMethod postMethod = new PostMethod(requestUrl);
byte[] authEncBytes = Base64.encodeBase64(apiAuthString.getBytes());// base加密
String authStringEnc = new String(authEncBytes);
// 设置请求头Authorization
postMethod.setRequestHeader("Authorization", "Basic " + authStringEnc);
// 设置请求头API_KEY
//postMethod.setRequestHeader("API_KEY", headerValue);
// 设置请求头 Content-Type
postMethod.setRequestHeader("Content-Type", "application/json");//TFS只支持json格式
postMethod.setRequestHeader("Accept", "api-version=5.1-preview.1");
// 将表单的值放入postMethod中
postMethod.setRequestBody(data.toString());
// 参数编码格式设置防止中文参数乱码
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 执行postMethod
int statusCode = 0;
try {
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
return null;
} else {
String str = "";
try {
str = postMethod.getResponseBodyAsString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
}
}