`
qihuiyong6
  • 浏览: 40873 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Apache HttpClient 代理、登陆访问百度开放平台

阅读更多
   最近做了下使用httpclient登陆百度开放平台获取GisPOI数据,途中遇到了一些问题。做完了和大家分享一下,有哪些理解不对的地方请大家指教。

功能:使用httpclient登陆百度账户访问开放平台获取POI数据
环境:httpclient: httpclient-3.1
开发工具:eclipse

引用的jar包:
引用

commons-httpclient-3.1.jar
commons-logging.jar
commons-codec-1.3.jar

java代码:
package org.qhy.test;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class TestInvokeBaiduLogin {

	public static void main(String[] args) throws HttpException, IOException {
		// 创建httpClient客户端
		HttpClient client = new HttpClient();
		// 百度的登陆url
		String loginUrl = "https://passport.baidu.com/v2/api/?login";
		// 设置代理
		String httpProxyHost = "10.1.xx.xx";
		int proxyPort = 1010;
		client.getHostConfiguration().setProxy(httpProxyHost, proxyPort);
		// 代理验证信息
		client.getParams().setAuthenticationPreemptive(true);
		UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
				"username", "password");
		client.getState().setProxyCredentials(AuthScope.ANY, creds);
		// 代理设置结束(如果不需要代理这段可以注释掉)
		// 登陆百度
		PostMethod psotMethod = new PostMethod(loginUrl);
		// 设置登陆参数
		NameValuePair[] data = {
				new NameValuePair("class", "login"),
				new NameValuePair("tpl", "mn"),
				new NameValuePair("tangram", "true"),
				new NameValuePair("isPhone", "false"),
				new NameValuePair("loginType", "1"),
				new NameValuePair("token", "f6994339731518f4c50d30acc10cbc6f"),
				new NameValuePair("callback",
						"parent.bdPass.api.login._postCallback"),
				new NameValuePair("username", "xxxxx"),
				new NameValuePair("password", "xxx") };
		psotMethod.setRequestBody(data);
		// HttpClient像浏览器一样接收 Cookie,
		client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
		client.executeMethod(psotMethod);
		Header[] haeders = psotMethod.getRequestHeaders();
		// 获取cookie
		Cookie[] cookies = client.getState().getCookies();
		String cookiesStr = "";
		for (Cookie c : cookies) {
			cookiesStr += c.toString() + ";";
		}
		System.out.println("psotMethod--response:"
				+ new String(psotMethod.getResponseBody(), "UTF-8"));
		psotMethod.releaseConnection();
		// 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https
		HttpMethod method = new GetMethod(
				"http://api.map.baidu.com/place/v2/search?&q="
						+ URLEncoder.encode("饭店", "UTF-8") + "&region="
						+ URLEncoder.encode("北京", "UTF-8")
						+ "&output=json&ak=1649c026d0cea10ccf5741348526cb44");
		method.setRequestHeader("Cookie", cookiesStr);
		method.getParams().setBooleanParameter(
				HttpMethodParams.SINGLE_COOKIE_HEADER, true);
		client.executeMethod(method);
		// 打印 服务器状态
		System.out.println("状态:" + method.getStatusLine());
		// 打印信息
		System.out.println("内容:"
				+ new String(method.getResponseBody(), "UTF-8"));
		// 关闭连接
		method.releaseConnection();
	}
}



运行结果:
引用

2014-4-23 17:28:26 org.apache.commons.httpclient.HttpMethodDirector authenticateHost
警告: Required credentials not available for BASIC <any realm>@api.map.baidu.com:80
2014-4-23 17:28:26 org.apache.commons.httpclient.HttpMethodDirector authenticateHost
警告: Preemptive authentication requested but no default credentials available
状态:HTTP/1.0 200 OK
内容:{
    "status":0,
    "message":"ok",
    "results":[
        {
            "name":"晚秋",
            "location":{
                "lat":39.963643,
                "lng":116.328947
            },
            "address":"海淀区中关村南大街9号理工科技大厦1楼",
            "street_id":"850815e174e4178243e19202",
            "uid":"850815e174e4178243e19202"
        },
        {
            "name":"蓝港饭庄",
            "location":{
                "lat":39.954199,
                "lng":116.479906
            },
            "address":"北京市朝阳区朝阳公园路6号",
            "street_id":"50fb64ffe16b1144e9f8536d",
            "telephone":"(010)59056668,(010)59056663",
            "uid":"50fb64ffe16b1144e9f8536d"
        },
        {
            "name":"上庄水库(北岸韩式饭庄)",
            "location":{
                "lat":40.10668,
                "lng":116.214358
            },
            "address":"上庄镇上庄水库大桥南岸往西400米",
            "street_id":"f633606af167004d0b69d92b",
            "telephone":"15801636650",
            "uid":"f633606af167004d0b69d92b"
        },
        {
            "name":"北京饭店",
            "location":{
                "lat":39.915258,
                "lng":116.416697
            },
            "address":"北京市东城区东长安街33号",
            "street_id":"b9e5d0b8a5ffcea581a12e38",
            "telephone":"(010)65137766",
            "uid":"b9e5d0b8a5ffcea581a12e38"
        },
        {
            "name":"翠宫饭店(康乐部)",
            "location":{
                "lat":39.981772,
                "lng":116.338918
            },
            "address":"海淀区知春路76号",
            "street_id":"af1ae56b4fb679124207c14f",
            "telephone":"(010)62628888",
            "uid":"af1ae56b4fb679124207c14f"
        },
        {
            "name":"北京特产(北京华夏明珠宾馆写字楼东)",
            "location":{
                "lat":39.901864,
                "lng":116.32936
            },
            "address":"广莲路13号附近",
            "street_id":"f1569f0df942537241d63e46",
            "telephone":"63274484",
            "uid":"f1569f0df942537241d63e46"
        },
        {
            "name":"乔家大院(建外大街店)",
            "location":{
                "lat":39.911266,
                "lng":116.449782
            },
            "address":"建华南路11号",
            "street_id":"923ffd004ae07aaed92f283e",
            "telephone":"(010)65661479",
            "uid":"923ffd004ae07aaed92f283e"
        },
        {
            "name":"西苑饭店",
            "location":{
                "lat":39.942936,
                "lng":116.33884
            },
            "address":"北京市海淀区三里河路1号(近北京首都体育馆和北京动物园)",
            "street_id":"eb3675036510d10201ec0197",
            "telephone":"010-68313388,010-68314577",
            "uid":"eb3675036510d10201ec0197"
        },
        {
            "name":"世纪金源大饭店",
            "location":{
                "lat":39.959377,
                "lng":116.287224
            },
            "address":"北京市北京海淀板井路69号",
            "telephone":"(010)88598888,13901257698",
            "uid":"1816533449feda1dfa2d619e"
        },
        {
            "name":"Burberry(金茂北京威斯汀大饭店南)",
            "location":{
                "lat":39.955057,
                "lng":116.466512
            },
            "address":"中国北京市朝阳区新源南路1号都汇天地首层L15号商铺",
            "street_id":"dffd631c68351da538221c5a",
            "telephone":"(010)84442261",
            "uid":"dffd631c68351da538221c5a"
        }
    ]
}
分享到:
评论

相关推荐

    apache httpclient jar包

    Apache HttpClient是一款广泛使用的Java库,专门用于执行HTTP请求。它属于Apache HttpComponents项目的一部分,旨在提供一个高效、灵活且可扩展的HTTP客户端API。在Java应用程序中,如果你需要与Web服务器进行交互,...

    apache httpclient 4.5.1 doc

    apache httpclient document apache httpcore document

    apache HttpClient jar包

    这个jar包是Apache HttpClient的核心组件,广泛应用于各种Java应用程序,尤其是那些需要与Web服务进行交互的场景。 在Apache HttpClient中,你可以: 1. **创建HTTP请求**:HttpClient允许你轻松地构造GET、POST...

    Apache httpclient源码4.5.12

    Apache HttpClient 是一个强大的Java库,用于执行HTTP请求。在4.5.12版本中,它提供了丰富的功能,包括支持HTTP/1.1和部分HTTP/2协议,连接管理,重试策略,以及多种认证机制。这个源码版本是学习HTTP通信、网络编程...

    apache httpclient源代码,eclipse查看源码

    - 在Eclipse 中,你可以通过"File" -&gt; "Import" -&gt; "Existing Projects into Workspace" 导入Apache HttpClient 4.5 的源代码项目。 - 将下载的压缩包解压,然后在导入对话框中选择解压后的目录,确保"Copy ...

    apache httpclient 源码和 jar包

    这个资源包括Apache HttpClient的源码和jar包,这意味着我们可以深入理解其内部工作原理并进行定制化开发。 源码部分: 1. **源码分析**:源码是未经反编译的,这意味着我们可以直接阅读和理解开发者原始的编程思路...

    [享学Feign] 九、Feign + OkHttp和Feign + Apache HttpClient哪个更香?

    目录前言正文OkHttp使用示例源码解析Apache HttpClient使用示例源码解析GoogleHttpClient哪个更香?总结声明 前言 前八篇文章介绍完了feign-core核心内容,从本篇开始将介绍它的“其它模块”。其实核心模块可以独立...

    Apache HttpClient Demo

    在阅读"Apache HttpClient Demo"这篇博客时,应重点关注示例代码,理解如何初始化HttpClient,构建请求,设置参数,发送请求,以及解析和处理响应。此外,还可以学习如何优化配置,如调整连接池大小,设置合适的超...

    HttpClient代理访问网络

    HttpClient代理访问网络

    wechatpay-apache-httpclient-0.2.1.jar

    wechatpay-apache-httpclient-0.2.1.jar

    Android使用Apache的HttpClient访问微Web站点

    本实例描述了Android平台下使用Apache开源项目的HttpClient访问网络的示例。分别使用HttpClient下的两个子实现类,DefaultHttpClient和AndroidHttpClient分别进行演示。

    HttpClient4基础1--通过匿名代理访问网页

    本篇将重点介绍HttpClient4的基础用法,特别是如何通过匿名代理来访问网页。 首先,让我们了解什么是HTTP代理。在互联网通信中,代理服务器充当了客户端与目标服务器之间的中介,可以用于匿名访问、负载均衡、缓存...

    可用org.apache.commons.httpclient-3.1.0.jar.zip

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....

    apache-httpclient应用所有jar

    在这个压缩包中,"apache-httpclient"包含了HttpClient所需的所有依赖。以下是一些基本的使用步骤: 1. **创建HttpClient实例**: 创建HttpClient对象是使用HttpClient的第一步。例如,可以使用`HttpClientBuilder`...

    httpclient4.3 设置代理

    javase http通讯技术 apache httpclient4.3 设置代理详解

    org.apache.commons.httpclient相关资源包

    Apache HttpClient 是一个强大的Java库,用于执行HTTP请求。在标题"org.apache.commons.httpclient相关资源包"中,我们可以看出这是关于使用Apache HttpClient进行HTTP通信的知识点。Apache HttpClient库是Apache...

    网络爬虫 apache HttpClient

    在阅读博文《网络爬虫 apache HttpClient》时,作者可能详细介绍了如何初始化HttpClient实例,设置请求参数,处理响应,以及如何利用HttpClient进行多线程爬虫开发。文章可能还会涵盖一些实战案例,比如抓取特定网站...

    apache代理服务器,包含二级代理的配置

    apache代理服务器,包含二级代理的配置

    Apache HttpClient 4.5.6

    Apache HttpComponents Client的4.5.6版本 ,下载引入工程即可。

Global site tag (gtag.js) - Google Analytics