httpclient 应用

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

这个东西很好用。

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

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

难点:处理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.iteye.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.iteye.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.iteye.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.iteye.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("状态结束");
}

}

你可能感兴趣的:(apache,浏览器,IE,Blog)