java postMethod

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>";
 }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值