<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(249) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 91精品免费国产高清在线| 日韩人妻一区二区三区免费| a级毛片免费网站| 一本久久a久久精品亚洲| 亚洲桃色AV无码| 亚洲精品和日本精品| 亚洲精品无码专区在线在线播放 | a一级毛片免费高清在线| 国产色爽免费无码视频| 免费无码国产在线观国内自拍中文字幕 | 国产成+人+综合+亚洲专| 日本亚洲成高清一区二区三区| 亚洲资源在线观看| 亚洲一卡2卡3卡4卡5卡6卡| 亚洲国产精品综合久久20| 亚洲AV香蕉一区区二区三区| 亚洲综合无码无在线观看| 免费毛片毛片网址| 99久久免费看国产精品| 午夜免费福利影院| 中文字幕一精品亚洲无线一区 | 亚洲综合色区在线观看| 免费一级e一片在线播放| 亚洲国产精品无码久久久不卡| 亚洲一级高清在线中文字幕| 亚洲国产日韩在线| 久久久亚洲AV波多野结衣| 亚洲国产高清在线精品一区| 精品国产亚洲第一区二区三区| 13小箩利洗澡无码视频网站免费 | 毛片免费在线观看网站| 亚洲伊人久久综合影院| 亚洲国产高清美女在线观看| 一级毛片在线完整免费观看| 四虎精品视频在线永久免费观看| 亚洲高清无码专区视频| 亚洲国产精品一区二区久| a级毛片免费网站| 午夜一区二区免费视频| 亚洲av日韩av激情亚洲| 久久精品国产亚洲av天美18 |