package com.test.servlet;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class TestPostMethod {
public static void main(String[] args) {
//向哪发送请求
PostMethod postMethod = new PostMethod("http://localhost:8080/TomSyncWeb/contactWebs!Test.html");
// postMethod.addParameter("advancedvalue1", "java");
// postMethod.addParameter("advancedfield1", "主题");
// postMethod.addParameter("yearstart", "1979");
NameValuePair[] data = {
//设置表单元素,和填值
new NameValuePair("contactLastName", "Lastname++"),
new NameValuePair("contactFirtsName", "Firstname++"),
};
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
HttpClient client = new HttpClient();
client.setConnectionTimeout(1000 * 60);
int status = 0;
try {
status = client.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (status == HttpStatus.SC_OK) {
try {
makeResponseObject(postMethod.getResponseBodyAsString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
}
postMethod.releaseConnection();
}
// 根据HTTP请求后返回的字串生成结果封装类的实例
private static void makeResponseObject(String sResponse) {
System.out.println("-------------------" + sResponse);
//得到相应的Response
System.out.println("-------------------");
String regExNums = "<TD\\s*style=\"FONT-SIZE:\\s*9pt;\\s*COLOR:\\s*#000000;\\s*WORD- WRAP:\\s*break-word\"\\s*vAlign=center\\s*bgColor=#f4f5f6\\s*height=30& gt;\\s*共有记录\\s*(\\d+)\\s*条\\s* \\s*</TD>";
String regExData = "<TR>\\s*<TD\\s*style=\"FONT-SIZE:\\s*9pt;\\s*WORD-WRAP:\\s*break-word\"\\s*(?:align=left)?\\s*bgColor=#\\S+\\s*>([\\s\\S]*?)</TR>";
String regExPage = "<TD\\s*style=\"FONT-SIZE:\\s*9pt;\\s*COLOR:\\s*#000000;\\s*WORD- WRAP:\\s*break-word\"\\s*vAlign=center\\s*bgColor=#f4f5f6\\s*height=30& gt;\\s*<A\\s*href=\"(.*)\">(?:上页|下页)</A>";
}
}