java something

不要以为......很遥远
随笔 - 23, 文章 - 1, 评论 - 2, 引用 - 0
数据加载中……

test

package client.test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class TIPOTest {

 private static String mainPageUrl = "http://twpat.tipo.gov.tw/twcgi/ttsweb?@0:0:1:twpat2@@"
   + Math.random();
 public static String strEncoding = "utf-8"; //strEncoding = "iso-8859-1";
 final static String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";

 public static void main(String[] args) throws Exception {
//  HttpClient client = new HttpClient();
//  client.getHostConfiguration().setProxy("10.41.16.98", 808);
//  client.getHostConfiguration().setHost(
//    "http://twpat3.tipo.gov.tw/tipotwoc", 80, "http");

//  String strLegalStatus = null;
  String mainPage = doGetMethod(mainPageUrl);

  String searchPageUrl = parserMainPage(mainPage);

  String searchPage = doGetMethod(searchPageUrl);

  String queryPageUrl = getQueryPageUrl(searchPage);

  String queryPage = doGetMethod(queryPageUrl);

  String searchResultUrl = parserPageForPostURL(queryPage);
  Map parameterMap = parseParameters(queryPage);

  System.out.println("searchResultUrl : " + searchResultUrl);

  String response = getResultPage(searchResultUrl, "087121847",
    parameterMap);

  // HttpMethod method = getPostMethod(); // 使用 POST 方式提交数据
  // // HttpMethod method = getGetMethod(); // 使用 GET 方式提交数据
  // client.executeMethod(method); // 打印服务器返回的状态
  // System.out.println(method.getStatusLine()); // 打印结果页面
  // String response = new
  // String(method.getResponseBodyAsString().getBytes(
  // "8859_1"));

  // 打印返回的信息
  System.out.println(response);
  // 释放连接
  // method.releaseConnection();
 }

 private static String doGetMethod(String url) throws Exception {
  try {
   HttpClient httpClient = new HttpClient();
   HostConfiguration hc = httpClient.getHostConfiguration();

   hc.setProxy("10.41.16.98", 808);
   httpClient.getState().setAuthenticationPreemptive(true);

   GetMethod getMethod = new GetMethod(url);

   getMethod.setRequestCharSet(strEncoding);
   getMethod.setResponseCharSet(strEncoding);
   getMethod.setRequestHeader("User-Agent", userAgent);
   getMethod.setTimeout(1000 * 60 * 2);
   int StatusCode = httpClient.executeMethod(getMethod);
   if (StatusCode >= 400) {
    getMethod.getConnection().close();
    throw new Exception("Can't get information.");
   }
   String str = getMethod.getResponseBodyAsString();
   getMethod.releaseConnection();

   writeStrToTxt(str);// wuwen add for test

   return str;
  } catch (Exception e) {
   e.printStackTrace();
   throw e;
  }
 }

 static int i = 1;

 // wuwen add for test
 public static void writeStrToTxt(String str) {
  i++;
  BufferedWriter output;
  try {
   output = new BufferedWriter(new FileWriter(
     "C:\\Documents and Settings\\F3223114\\桌面\\" + i + ".txt"));

   output.write(str);
   output.close();

  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private static String parserMainPage(String body) {
  String parserSearchUrl = "";
  /*
   * String regEx = "href=(.+?)\\s*class=menu"; Pattern pattern =
   * Pattern.compile(regEx, Pattern.CASE_INSENSITIVE); Matcher matcher =
   * pattern.matcher(body); while (matcher.find()) { if
   * (matcher.group(1).contains(":80:::0")) { parserSearchUrl =
   * matcher.group(1); } }
   */
  int i = body.indexOf("http");
  if (i != -1) {
   parserSearchUrl = body.substring(i, body.indexOf(">", i));
  }
  return parserSearchUrl;
 }

 public String getMidString(String sInput, String sStartTag, String sEndTag) {
  int i, j = -1;
  if ((i = sInput.indexOf(sStartTag)) < 0) {
   return null;
  } else {
   if ((j = sInput.indexOf(sEndTag, i + sStartTag.length())) < 0) {
    return null;
   } else {
    if (i + sStartTag.length() == j) {
     return null;
    } else {
     return sInput.substring(i + sStartTag.length(), j);
    }
   }
  }
 }

 private static String doPostMethod(String url, NameValuePair[] nameValuePair)
   throws Exception {
  try {
   HttpClient httpClient = new HttpClient();
   HostConfiguration hc = httpClient.getHostConfiguration();
   hc.setProxy("10.41.16.98", 808);
   httpClient.getState().setAuthenticationPreemptive(true);

   PostMethod postMethod = new PostMethod(url);
   postMethod.setRequestCharSet(strEncoding);
   postMethod.setResponseCharSet(strEncoding);
   postMethod.setRequestHeader("User-Agent", userAgent);
   postMethod.setTimeout(1000 * 60 * 2);
   postMethod.setRequestBody(nameValuePair);
   int StatusCode = httpClient.executeMethod(postMethod);
   if (StatusCode >= 400) {
    postMethod.getConnection().close();
    throw new Exception("Can't get information.");
   }
   String str = postMethod.getResponseBodyAsString();

   postMethod.releaseConnection();
   return str;
  } catch (Exception e) {
   e.printStackTrace();
   throw e;
  }
 }

 // modify by zhuoluo 2008.12.09
 private static String getResultPage(String url, String strAppNo,
   Map parameterMap) throws Exception {
  try {
   NameValuePair _0_1_T = new NameValuePair("@_0_1_T", "T_UID");
   _0_1_T.setHeadNeedEncode(false);
   NameValuePair _0_1_T_1 = new NameValuePair("_0_1_T", "");
   _0_1_T_1.setHeadNeedEncode(false);
   NameValuePair _0_2_P = new NameValuePair("@_0_2_P", "P_PWD");
   _0_2_P.setHeadNeedEncode(false);
   NameValuePair _0_2_P_1 = new NameValuePair("_0_2_P", "");
   _0_2_P_1.setHeadNeedEncode(false);
   NameValuePair _20_0_T = new NameValuePair("@_20_0_T", "T_AN");
   _20_0_T.setHeadNeedEncode(false);
   NameValuePair appNo = new NameValuePair("_20_0_T", strAppNo);
   appNo.setHeadNeedEncode(false);
   NameValuePair _20_1_K = new NameValuePair("@_20_1_K", "K_NE");
   _20_1_K.setHeadNeedEncode(false);
   NameValuePair _20_1_K_1 = new NameValuePair("_20_1_K", "");
   _20_1_K_1.setHeadNeedEncode(false);
   NameValuePair JPAGE = new NameValuePair("JPAGE", "");
   JPAGE.setHeadNeedEncode(false);

   NameValuePair INFO = new NameValuePair("INFO", parameterMap.get(
     "INFO").toString());
   INFO.setHeadNeedEncode(false);

//   NameValuePair x = new NameValuePair("_IMG_%E6%AA%A2%E7%B4%A2.x",
//     "39");
//   x.setHeadNeedEncode(false);
//   NameValuePair y = new NameValuePair("_IMG_%E6%AA%A2%E7%B4%A2.y",
//     "10");
//   y.setHeadNeedEncode(false);
   NameValuePair x = new NameValuePair("_IMG_%E6%AA%A2%E7%B4%A2%25z.x",
   "39");
   x.setHeadNeedEncode(false);
   NameValuePair y = new NameValuePair("_IMG_%E6%AA%A2%E7%B4%A2%25z.y",
   "10");
   y.setHeadNeedEncode(false);

   NameValuePair[] nameValuePair = new NameValuePair[] { _0_1_T,
     _0_1_T_1, _0_2_P, _0_2_P_1, _20_0_T, appNo, _20_1_K,
     _20_1_K_1, x, y , INFO };
//   NameValuePair[] nameValuePair = new NameValuePair[] { _0_1_T,
//     _0_1_T_1, _0_2_P, _0_2_P_1, _20_0_T, appNo, _20_1_K,
//     _20_1_K_1, JPAGE, INFO };//x, y ,
   String str = doPostMethod(url, nameValuePair);

   writeStrToTxt(str);// wuwen add for test

   return str;
  } catch (Exception e) {
   e.printStackTrace();
   throw e;
  }
 }

 private static String getQueryPageUrl(String body) {
  Parser parser = Parser.createParser(body, "utf-8");
  NodeFilter filter_constellation_summart = new AndFilter(
    (new TagNameFilter("a")), (new HasAttributeFilter("class",
      "menu")));
  NodeList nodeList = null;
  String href = "";
  try {
   nodeList = parser
     .extractAllNodesThatMatch(filter_constellation_summart);
   Node node = null;
   node = nodeList.elementAt(3);
   href = ((TagNode) node).getAttribute("href");
  } catch (ParserException ex) {
  }

  return href;
 }

 private static String parserPageForPostURL(String body)
   throws ParserException {
  String searchResultUrl = "";
  NodeFilter tagFilter = new TagNameFilter("FORM");
  Parser parser = Parser.createParser(body, "UTF-8");
  NodeList list = parser.extractAllNodesThatMatch(tagFilter);
  for (int i = 0; i < list.size(); i++) {
   Node node = list.elementAt(i);
   if (node instanceof TagNode) {
    String methodValue = ((TagNode) node).getAttribute("METHOD");
    if (methodValue != null) {
     String actionPath = ((TagNode) node).getAttribute("ACTION");
     searchResultUrl = actionPath;
    }
   }
  }
  return searchResultUrl;
 }

 private static Map<String, String> parseParameters(String pageBody)
   throws ParserException {
  Map<String, String> parameterMap = new HashMap<String, String>();
  NodeFilter tagFilter = new TagNameFilter("input");
  Parser parser = Parser.createParser(pageBody, "UTF-8");
  NodeList list = parser.extractAllNodesThatMatch(tagFilter);
  for (int i = 0; i < list.size(); i++) {
   Node node = list.elementAt(i);
   if (node instanceof TagNode) {
    String nodeName = ((TagNode) node).getAttribute("name");
    String nodeValue = ((TagNode) node).getAttribute("value");

    if (nodeName != null) {
     if (nodeName.equals("INFO")) {
      if (nodeValue == null) {
       nodeValue = "";
      }
      parameterMap.put(nodeName, nodeValue);
     }
    }
   }
  }
  return parameterMap;
 }
}

posted on 2011-01-11 17:42 Jamie 阅读(246) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: