当前位置: 代码迷 >> Java Web开发 >> httpclient post 请求一直超时,求解?解决方案
  详细解决方案

httpclient post 请求一直超时,求解?解决方案

热度:695   发布时间:2016-04-16 22:23:59.0
httpclient post 请求一直超时,求解??
最近在做一个接口对接,用的是httpclient的post方式请求,一直是报超时,url直接在浏览器中访问时却可以访问,不懂为什么??

于是我吧url改成百度,但是还是一直报超时,不清楚了,各位帮忙看下

代码如下
public class HttpClientUtils {

public static void main(String arg[]) throws Exception {
String url = "http://www.baidu.com/";
// getDoGetURL2(url,"utf-8");//测试ok
// getDoGetURL(url,"utf-8");//测试ok
getDoPostResponseDataByURL(url, null, "utf-8", true); // 测试ok

// WebUtils.doPost(url, null, 30000, 30000);
}
/**
 * <p>
 * 执行一个HTTP POST请求,返回请求响应的HTML
 * </p>
 * 
 * @param url
 *            请求的URL地址
 * @param params
 *            请求的查询参数,可以为null
 * @param charset
 *            字符集
 * @param pretty
 *            是否美化
 * @return 返回请求响应的HTML
 */
public static String getDoPostResponseDataByURL(String url,
Map<String, String> params, String charset, boolean pretty) {

StringBuffer response = new StringBuffer();

HttpClient client = new HttpClient();
HttpMethod method = new PostMethod(url);

// 设置Http Post数据
if (params != null) {
HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
}
method.setParams(p);
}
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
// 读取为 InputStream,在网页内容数据量大时候推荐使用
BufferedReader reader = new BufferedReader(
new InputStreamReader(method.getResponseBodyAsStream(),
charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(
System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (IOException e) {
System.out.println("执行HTTP Post请求" + url + "时,发生异常!");
e.printStackTrace();
} finally {
method.releaseConnection();
}
System.out.println("--------------------" + response.toString());
return response.toString();
}

}
------解决方案--------------------
  相关解决方案