📄 myhttpclient.java
字号:
package com.eline.vod.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
* Http Client工具类
* @author Lucifer
*
*/
public class MyHttpClient {
private static MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
// Parameter settings
private static int connectionTimeOut = 20000;
private static int socketTimeOut = 10000;
private static int maxConnectionPerHost = 5;
private static int maxTotalConnections = 40;
// Is initialized?
private static boolean initialed = false;
public static void init() {
manager.getParams().setConnectionTimeout(connectionTimeOut);
manager.getParams().setSoTimeout(socketTimeOut);
manager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
manager.getParams().setMaxTotalConnections(maxTotalConnections);
initialed = true;
}
public static String getGetResponseAsString(String url, String encode) {
HttpClient client = new HttpClient(manager);
if (initialed) {
MyHttpClient.init();
}
GetMethod get = new GetMethod(url);
get.setFollowRedirects(true);
String result = null;
StringBuffer resultBuffer = new StringBuffer();
try {
client.executeMethod(get);
// 在目标页面情况未知的条件下,不推荐使用getResponseBodyAsString()方法
// String strGetResponseBody = post.getResponseBodyAsString();
BufferedReader in = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(),
get.getResponseCharSet()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}
in.close();
result = resultBuffer.toString();
// iso-8859-1 is the default reading encode
result = MyHttpClient.converterStringEncode(resultBuffer.toString(),
get.getResponseCharSet(), encode);
} catch (Exception e) {
e.printStackTrace();
result = "";
} finally {
get.releaseConnection();
}
return result;
}
public static String getPostResponseAsString(String url,
String encode, NameValuePair[] nameValuePair) {
HttpClient client = new HttpClient(manager);
if (initialed) {
MyHttpClient.init();
}
PostMethod post = new PostMethod(url);
post.setRequestBody(nameValuePair);
post.setFollowRedirects(false);
String result = null;
StringBuffer resultBuffer = new StringBuffer();
try {
client.executeMethod(post);
BufferedReader in = new BufferedReader(new InputStreamReader(
post.getResponseBodyAsStream(), post.getResponseCharSet()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}
in.close();
// iso-8859-1 is the default reading encode
result = MyHttpClient.converterStringEncode(
resultBuffer.toString(), post.getResponseCharSet(), encode);
} catch (Exception e) {
e.printStackTrace();
result = "";
} finally {
post.releaseConnection();
}
return result;
}
private static String converterStringEncode(String src, String srcEncode,
String destEncode) {
if (src != null) {
try {
return new String(src.getBytes(srcEncode), destEncode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
} else {
return "";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -