直接贴代码
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .X509TrustManager;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class HttpUtil {
public static String httpPost(String url,Map<String, String> parametes){
String str = "";
HttpClient httpClient = new HttpClient();
PostMethod psotMethod = new PostMethod(url);
if(parametes!=null&&()>0){
Set<String> keys = ();
for(String key:keys){
(key, (key));//这里添加参数
}
}
().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");//请求编码
().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
//执行getMethod
int statusCode = (psotMethod);
/*if (statusCode != HttpStatus.SC_OK) {
("Method failed: "+ ());
}*/
//读取内容
byte[] responseBody = ();
str = new String(responseBody,"utf-8");
} catch (HttpException e) {
// TODO Auto-generated catch block
();
} catch (IOException e) {
// TODO Auto-generated catch block
();
} finally {
//释放连接
();
}
return str;
}
public static String httpGet(String url){
//(url);
String str = "";
//构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//创建GET方法的实例
GetMethod getMethod = new GetMethod(url);
//使用系统提供的默认的恢复策略
().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
//执行getMethod
int statusCode = (getMethod);
if (statusCode != HttpStatus.SC_OK) {
("Method failed: "
+ ());
}
//读取内容
byte[] responseBody = ();
//处理内容
str = new String(responseBody,"utf-8");
} catch (HttpException e) {
// TODO Auto-generated catch block
();
} catch (IOException e) {
// TODO Auto-generated catch block
();
} finally {
//释放连接
();
}
return str;
}
public static StringBuffer httpsSendGet(String path)
{
StringBuffer sb = new StringBuffer();
// String path = "https://kyfw./otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2015-05-22&from_station=BJP&to_station=ZZF";
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public .X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
.X509Certificate[] certs, String authType) {
}
} };
try {
SSLContext sc = ("SSL");
(null, trustAllCerts, new ());
(());
} catch (Exception e) {
}
try {
URL url = new URL(path);
HttpsURLConnection conn = (HttpsURLConnection)();
BufferedReader rd = new BufferedReader(new InputStreamReader((),"UTF-8"));
String line;
while ((line = ()) != null) {
//(line);
(line);
}
();
} catch (MalformedURLException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
();
}
return sb;
}
}
jar包地址
/detail/huidanyige/8603109