熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java 發Post請求實例程序代碼

2013-11-23 18:48:45  來源: Java核心技術 

  //說明使用java的HttpConnection 模擬發送post請求

  //該程序用來抓成績公布網站上的指定范圍准考證號的成績情況

  import javaioDataInputStream;
    import javaioDataOutputStream;
    import javaioInputStream;
    import javaioOutputStream;
    import HttpURLConnection;
    import URL;
    import javautilVector;

  public class GetMain {

  /**
      * @param args
      */
     private static String URL = ;
     private static String url=;

  public static void main(String[] args) {

  //Post Data 為Form提交的內容
       String postData=zkzh=+l+i+j+k+&tijiaox=&tijiaoy=;
       String html=GetResponseDataByID(URLpostData);
       String[] data=getNeedData(html);

  }
     }

  public static void PrintStrs(String[] str)
    {
     for(String s:str)
     {
      Systemoutprint(s+);
     }
     Systemoutprintln();
    }

  //通過url和postdata獲取返回值該函數為主要函數可以copy使用返回值為一個Html頁面的數據
     public static String GetResponseDataByID(String urlString postData)
     {
      String data=null;
      try {
       URL dataUrl = new URL(url);
       HttpURLConnection con = (HttpURLConnection) dataUrlopenConnection();
       consetRequestMethod(POST);
       consetRequestProperty(ProxyConnection KeepAlive);
       consetDoOutput(true);
       consetDoInput(true);

  OutputStream os=congetOutputStream();
       DataOutputStream dos=new DataOutputStream(os);
       doswrite(postDatagetBytes());
       dosflush();
       dosclose();

  InputStream is=congetInputStream();
       DataInputStream dis=new DataInputStream(is);
       byte d[]=new byte[disavailable()];
       disread(d);
       data=new String(d);
       //Systemoutprintln(data);
       condisconnect();
      } catch (Exception ex) {
       exprintStackTrace();
      }
      return data;
     }

  /**Get Needed Data Form Responsed Html File**/
     static String I=准考證號;
     static String I=姓 名;
     static String I=級別;
     static String I=筆試成績;
     static String I=口試成績;

  public static String[] getNeedData(String html)
     {
      if(html==null)
       return null;
      String[] str=new String[];
      str[]=getPart(htmlI);
      str[]=getPart(htmlI);
      str[]=getPart(htmlI);
      str[]=getPart(htmlI);
      str[]=getPart(htmlI);

  return str;
     }

  public static String getPart(String sourceString type)
     {
      if(source==null)
       return null;
      if(sourceindexOf(准考證號輸入錯誤)!=)
       return null;
      if(sourceindexOf(type)!=)
      {
       source=sourcesubstring(sourceindexOf(type)+typelength());
       source=sourcesubstring(sourceindexOf(</TD></TR>));
       source=sourcereplace(</TD> );
       source=sourcereplace(<TD> );
       source=sourcereplace(<TD align=center > );
       source=sourcereplace(<TD align=center> );
       source=sourcereplace(\n);
       return sourcetrim();
      }
      else
       return null;
     }
    }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25779.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.