httpClient(1)
来源:互联网 发布:简单的数据库设计软件 编辑:程序博客网 时间:2024/06/11 17:44
public static String doPost(String url, NameValuePair[] params,
String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
String proxyHost = "192.168.19.9";
int proxyPort = 80;
String userName = "yellowpage\\yellowpage_jpsc";
String password = "abcd_123";
/*
client.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(userName, password));
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
*/
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
client.getParams().setAuthenticationPreemptive(true);
//如果代理需要密码验证,这里设置用户名密码
client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName,password));
PostMethod 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.setRequestBody(params);
}
try {
client.executeMethod(method);
log.info("http response status code: " + method.getStatusCode());
if (method.getStatusCode() == HttpStatus.SC_OK) {
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) {
log.error(
"request url exception: " + url + " params: {" + params != null ? params
.toString() : "" + "}", e);
} finally {
method.releaseConnection();
}
return response.toString();
}
String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
String proxyHost = "192.168.19.9";
int proxyPort = 80;
String userName = "yellowpage\\yellowpage_jpsc";
String password = "abcd_123";
/*
client.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(userName, password));
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
*/
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
client.getParams().setAuthenticationPreemptive(true);
//如果代理需要密码验证,这里设置用户名密码
client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName,password));
PostMethod 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.setRequestBody(params);
}
try {
client.executeMethod(method);
log.info("http response status code: " + method.getStatusCode());
if (method.getStatusCode() == HttpStatus.SC_OK) {
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) {
log.error(
"request url exception: " + url + " params: {" + params != null ? params
.toString() : "" + "}", e);
} finally {
method.releaseConnection();
}
return response.toString();
}
0 0
- httpclient 1
- httpClient(1)
- annotation与HttpClient(1)--HttpClient简介
- java httpclient(1)
- httpClient学习笔记1
- HttpClient学习(1)
- HttpClient 学习1
- HttpClient的应用1
- HttpClient学习使用(1)
- HttpClient 讲解 (1) 基础
- HttpClient 学习整理1
- httpclient 4.5.1---基础
- ★★ httpclient-3.1 TO HttpClient 4.0.1,httpclient-3.1 升级到HttpClient 4.0.1
- HttpClient
- HttpClient
- httpClient
- HttpClient
- HttpClient
- PyMC3
- 微信公众号开发-log4j
- Java 使用 Redis
- ReactNative ListView基础功能
- 字符串转CGfloat
- httpClient(1)
- 二维码-去掉原始的HistoryManager也就是历史记录
- 美图面试c/c++内存分配方式不同
- Ubuntu安装搜狗中文输入法
- 面试中的 Singleton
- 09_组合在一起
- ReactNative ListView + 上拉加载更多
- Mybatis Generator(简称MBG)的最完整配置文件
- socket阻塞与非阻塞,同步与异步、I/O模型