Login 如何使用Apache';什么是HttpClient?
我想登录我的gmail并通过httpClient自动获取联系人列表, 我尝试了下面页面中描述的方法: 但一旦它运行到:Login 如何使用Apache';什么是HttpClient?,login,gmail,httpclient,contacts,Login,Gmail,Httpclient,Contacts,我想登录我的gmail并通过httpClient自动获取联系人列表, 我尝试了下面页面中描述的方法: 但一旦它运行到: String cookie=response.getFirstHeader(“设置cookie”).getValue() 捕获到java.lang.NullPointerException。 我认为这是因为页面被临时移动,然后我将代码编码如下: private static String uriLogin = "https://mail.google.com"; pri
String cookie=response.getFirstHeader(“设置cookie”).getValue()代码>
捕获到java.lang.NullPointerException。
我认为这是因为页面被临时移动,然后我将代码编码如下:
private static String uriLogin = "https://mail.google.com";
private static String uriContacts = "https://mail.google.com/mail/shva=1#contacts";
// the account was registered just for test:
private static String myAcc = "httpclient.test";
private static String myPwd = "testpassword";
public static void main(String[] args) throws ClientProtocolException,
IOException, InterruptedException {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(uriLogin);
List<NameValuePair> params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("Email", myAcc));
params.add(new BasicNameValuePair("Passwd", myPwd));
params.add(new BasicNameValuePair("signIn", "Sign in"));
post.setEntity(new UrlEncodedFormEntity(params));
redp("url is:: ", post.getEntity());
HttpResponse rsp = client.execute(post);
if (rsp.getStatusLine().getStatusCode() >= 400) {// returns 302 if
// success
System.err.println("failed to get the web page!!!");
System.err.println("status: " + rsp.getStatusLine());
System.exit(-1);
}
redp("status is:: ", rsp.getStatusLine());
redp("heads of rsp :: ", "");
pHeads(rsp);
redp("content is:: ", EntityUtils.toString(rsp.getEntity()));
String redirect = rsp.getLastHeader("Location").getValue();
HttpGet get = new HttpGet(redirect);
rsp = client.execute(get);
String cookie = rsp.getFirstHeader("Set-Cookie").getValue();
redp("cookie is:: ", cookie);
HttpGet getContacts = new HttpGet(uriContacts);
getContacts.setHeader("Cookie", cookie);
redp("heads of get [contacts]:: ", "");
pHeads(getContacts);
client.getConnectionManager().shutdown();
client = new DefaultHttpClient(); // without these 2 lines,
// "java.lang.IllegalStateException"
// will be catched
rsp = client.execute(getContacts);
redp("heads of rsp (new) ::", "");
pHeads(rsp);
InputStream istream = rsp.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
istream));
String line;
p("联系人列表: ");
while ((line = reader.readLine()) != null) {
p(line);
}
reader.close();
istream.close();
}// main
public static void pHeads(HttpMessage msg) {
Header[] headers = msg.getAllHeaders();
for (int i = 0; i < headers.length; i++)
p(headers[i].getName() + ": " + headers[i].getValue());
}
public static void p(Object o) {
System.out.println(o);
}
public static void redp(String head, Object o) throws InterruptedException {
System.err.println(head);
if (o.equals("") || o.equals(null))
return;
Thread.sleep(100);
System.out.println(o);
}
private静态字符串uriLogin=”https://mail.google.com";
专用静态字符串uriContacts=”https://mail.google.com/mail/shva=1#contacts";
//该帐户仅注册用于测试:
私有静态字符串myAcc=“httpclient.test”;
私有静态字符串myPwd=“testpassword”;
公共静态void main(字符串[]args)抛出ClientProtocolException,
IOException,InterruptedException{
DefaultHttpClient=新的DefaultHttpClient();
HttpPost=新的HttpPost(uriLogin);
列表参数=新的ArrayList(3);
参数添加(新的BasicNameValuePair(“电子邮件”,myAcc));
参数add(新的BasicNameValuePair(“Passwd”,myPwd));
参数添加(新的BasicNameValuePair(“登录”);
post.setEntity(新的UrlEncodedFormEntity(params));
redp(“url为::”,post.getEntity());
HttpResponse rsp=client.execute(post);
if(rsp.getStatusLine().getStatusCode()>=400){//返回302 if
//成功
System.err.println(“获取网页失败!!!”;
System.err.println(“状态:+rsp.getStatusLine());
系统退出(-1);
}
redp(“状态为::”,rsp.getStatusLine());
redp(“rsp负责人::”,”);
pHeads(rsp);
redp(“内容是::”,EntityUtils.toString(rsp.getEntity());
字符串重定向=rsp.getLastHeader(“位置”).getValue();
HttpGet=newhttpget(重定向);
rsp=client.execute(get);
字符串cookie=rsp.getFirstHeader(“设置cookie”).getValue();
redp(“cookie是::”,cookie);
HttpGet-getContacts=新的HttpGet(uriContacts);
setHeader(“Cookie”,Cookie);
redp(“获取[联系人]::,”)的负责人;
pHeads(获取联系人);
client.getConnectionManager().shutdown();
client=new DefaultHttpClient();//如果没有这两行,
//“java.lang.IllegalStateException”
//会被抓住的
rsp=client.execute(getContacts);
区域市政总署(“区域市政总署署长(新):”、“);
pHeads(rsp);
InputStream istream=rsp.getEntity().getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
峡流);
弦线;
p(“联系人列表: ");
而((line=reader.readLine())!=null){
p(直线);
}
reader.close();
istream.close();
}//主要
公共静态无效pHeads(HttpMessage msg){
Header[]headers=msg.getAllHeaders();
对于(int i=0;i
}
`
但它仍然不起作用…任何帮助都会很好~~
[顺便说一句,我在互联网上看到一条消息,有人说httpClient不太适合做这种工作,你能告诉我httpClient在什么样的项目中使用得最多吗?]据我所知,当你得到响应时,你会收到NullPointerException。getLastHeader(“Location”).getValue();
GMail html头中似乎没有“Location”标记。当Java找不到它时,它会抛出“我找不到它”
您需要的是POST发生后的重定向。
快速一看,它有点像:
但是你应该仔细检查一下。
希望这有帮助