主要是设置http头
在HostConfiguration.setProxy里设置。如下面代码:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientUse {
public static void main(String[] args) throws HttpException, IOException {
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setProxy("localhost", 808);
/*//需要验证
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("chenlb", "123456");
httpClient.getState().setProxyCredentials(AuthScope.ANY, creds);
*/
//设置http头
List <Header> headers = new ArrayList <Header>();
headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
GetMethod method = new GetMethod("http://www.baidu.com");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
int statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Method failed code="+statusCode+": " + method.getStatusLine());
} else {
System.out.println(new String(method.getResponseBody(), "gb2312"));
}
} finally {
method.releaseConnection();
}
}
}
如果要用户名与密码验证的,请把/* */注释去掉。使验证有效。
验证,关键是:
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("chenlb", "123456");
httpClient.getState().setProxyCredentials(AuthScope.ANY, creds);
设置http请求头.
List <Header> headers = new ArrayList <Header>();
headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
详细解决方案
httpclient 账号密码 验证
热度:75 发布时间:2024-01-10 11:01:36.0
相关解决方案
- httpclient 抓取不到网页内容是什么原因解决方案
- HttpClient 抓取网页js生成内容的有关问题。做过的侠客们。来看看
- httpclient 四抓取网页
- org.apache.http.client.HttpClient与org.apache.commons.httpclient.HttpClient的差别
- 【研究】httpclient 个别汉字乱码,该怎么解决
- HttpClient 跟 HttpServlet的区别
- httpclient 新浪微博 图片下传
- httpClient 错误 The server failed to respond with a valid HTTP response
- Htmlunit / HttpClient 报 Cookie rejected 警告 的解决方案
- 应用 HttpClient 和 HtmlParser 实现简易爬虫
- HTTPCLIENT+NEKOHTML+XPATH采撷网页数据
- 运用 HttpClient 和 HtmlParser 实现简易爬虫
- 应用 HttpClient 和 HtmlParser 实现简易爬
- Android 用 HttpClient 抓取 html 页面内容的步骤
- HttpClient 初懂得
- 运用JACKSON解析JSON(HttpClient 3处理请求)
- httpclient 的有关应用
- 运用 HttpClient 来对付各种顽固的WEB服务器
- 求教关于 HttpClient 抓取页面回到403,请大神赐教啊
- 【研究】httpclient 少数汉字乱码
- httpclient post 爬虫假死的诡异情况
- HttpClient 需要的jar包哪位高手给份啊 多谢各位大神了
- httpclient 4.1.3 交付https登录表单失败
- HttpClient,PostMethod返回值乱码有关问题
- httpclient post有关问题
- httpclient post上传图片的有关问题
- []Httpclient POST遇到未将对象引用设置到对象的实例
- httpclient post提交json乱码有关问题
- 求教关于 HttpClient 抓取页面回来403,请大神赐教啊
- httpclient post 请求一直超时,求解?解决方案