httpclient 应用

这个东西很好用。

对于跨系统的应用来说,可以实现单点登录

尤其是一个应用登录另一个应用

难点:处理cookie

 

下面是我的例子:

package com.fox;

import java.io.IOException;

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.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

public class main {

 /**
  * @param args
  * @throws IOException
  * @throws HttpException
  */
 public static void main(String[] args) throws HttpException, IOException {

  // 生成客户端:相当于打开IE窗口
  HttpClient client = new HttpClient();
  // 设置HTTPClient为Cookie兼容模式
  client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
  // 输入URL
  PostMethod post = new PostMethod("http://www.javaeye.com/login/");
  // 为文本框输入数据
  NameValuePair userid = new NameValuePair("name", "XXX");
  NameValuePair Password = new NameValuePair("password", "XXX");
  // 放到请求中
  post.setRequestBody(new NameValuePair[] { userid, Password });
  // 回车--返回状态
  int status = client.executeMethod(post);
  // X掉浏览器
  post.releaseConnection();
  /*
   * 下面是关键,要想让两次请求有关联,设置cookie
   */
  Cookie[] questCookie = client.getState().getCookies();
  if (questCookie.length > 0)
   // 把Cookie加到httpclient中
   client.getState().addCookies(questCookie);
  // 好了
  GetMethod getMethod = new GetMethod("http://www.javaeye.com/");
  int statusCode = client.executeMethod(getMethod);
  // System.out.println("response=" + getMethod.getResponseBodyAsString());
  Cookie[] questCookie2 = client.getState().getCookies();

  if (questCookie.length > 0)
   // 把Cookie加到httpclient中
   client.getState().addCookies(questCookie2);
  getMethod.releaseConnection();
  GetMethod postMethod2 = new GetMethod("http://lxxstarii.javaeye.com/admin/blogs/new");
  int statusCode2 = client.executeMethod(postMethod2);
  System.out.println("response=" + postMethod2.getResponseBodyAsString());
  postMethod2.releaseConnection();

  getRequestHeaders(postMethod2);
  getStatus(statusCode2);
  getResponseHeaders(postMethod2);

  Cookie[] questCookie3 = client.getState().getCookies();

  if (questCookie.length > 0)
   // 把Cookie加到httpclient中
   client.getState().addCookies(questCookie3);

  GetMethod post2 = new GetMethod("http://lxxstarii.javaeye.com/admin/blogs");
  NameValuePair title = new NameValuePair("blog_title", "test");
  NameValuePair autosave = new NameValuePair("auto_save_id", "editor_auto_save_id");
  NameValuePair body = new NameValuePair("editor_body", "body");
  // post2.setRequestBody(new NameValuePair[] { title, body ,autosave});
  int status3 = client.executeMethod(post2);
  System.out.println("response=" + post2.getResponseBodyAsString());
  post.releaseConnection();

  getRequestHeaders(post2);
  getStatus(status3);
  getResponseHeaders(post2);

 }

 // 得到Requestheader信息
 public static void getRequestHeaders(HttpMethodBase method) {
  Header[] headers = method.getRequestHeaders();
  System.out.println("Request头信息开始");
  for (int i = 0; i < headers.length; i++) {
   System.out.print(headers[i].getName() + ":::::");
   System.out.println(headers[i].getValue());
  }
  System.out.println("Request头信息结束");
 }

 // 得到Responseheader信息
 public static void getResponseHeaders(HttpMethodBase method) {
  Header[] headers = method.getResponseHeaders();
  System.out.println("Response头信息开始");
  for (int i = 0; i < headers.length; i++) {
   System.out.print(headers[i].getName() + ":");
   System.out.println(headers[i].getValue());
  }
  System.out.println("Response头信息结束");
 }

 public static void getStatus(int status) {
  System.out.println("状态开始");
  System.out.println("statusCode=" + status);
  System.out.println("状态结束");
 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值