/**
*Login page with httpclient3.1
*/
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class JLoginTest
{
public static void main(String args[])
throws HttpException, IOException
{
// ·¢̍postȫȳµȂ½
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("cws.namtai.com.cn", 80, "http");
PostMethod post = new PostMethod("/names.nsf?Login");
NameValuePair name = new NameValuePair("Username", "lmyin");
NameValuePair pass = new NameValuePair("Password", "yyts619");
//ЂĦµŖµ¶ԶՓڵ¥´¿µĵȂ½²»ˇ±ِ趄¡£
NameValuePair param5 = new NameValuePair("domain", "ntsz");
NameValuePair param6 = new NameValuePair("language", "zh-cn");
//½«ֵ¶ԼԈ숫ȳͥ
post.setRequestBody(new NameValuePair[] {name, pass ,param5,param6});
int status = client.executeMethod(post);
post.releaseConnection();
// ҩ֤cookie
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie cookies[] = cookiespec.match("cws.namtai.com.cn", 80, "/", false, client.getState().getCookies());
if(cookies.length == 0)
{
System.out.println("None");
} else
{
for(int i = 0; i < cookies.length; i++)
System.out.println((new StringBuilder()).append("========================================================/n").append(cookies[i].toString()).append("/n========================================================").toString());
}
// ¿ªʼȫȳҳĦ
GetMethod get = new GetMethod("http://cws.namtai.com.cn/lks/koa/lks_d2601.nsf/VD_ByClass/338BFFBD6070EA9A4825742500210E94?opendocument&m_Seq=0.9021852269941022");
get.setRequestHeader("Content-type", "text/html; charset=UTF-8");
client.executeMethod(get);
byte responseBody[] = get.getResponseBody();
String response = new String(responseBody);
File f=new File("JLoginTest.htm");
FileOutputStream fos=new FileOutputStream(f);
fos.write(responseBody);
fos.close();
System.out.println((new StringBuilder()).append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/n").append(response).append("/n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++").toString());
get.releaseConnection();
}
}