java模擬get/post提交

1:用jdk連接
		String action = "xxxxxxxxxxx";
		URL url = new URL(action);
		HttpURLConnection http = (HttpURLConnection) url.openConnection();
		http.setRequestMethod("POST");
		http.setConnectTimeout(0);
		http.setInstanceFollowRedirects(true);
		http.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");
		http.setDefaultUseCaches(false);
		http.setDoOutput(true);
		
		String queryString = "";
		PrintWriter out = new PrintWriter(http.getOutputStream());
		out.print(queryString);//傳入參數
		out.close();
		http.connect();//連接
		InputStream in = httpURLConnection.getInputStream();
2:apache組件
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.util.Map;  
  5.   
  6. import org.apache.commons.httpclient.HttpClient;  
  7. import org.apache.commons.httpclient.HttpMethod;  
  8. import org.apache.commons.httpclient.HttpStatus;  
  9. import org.apache.commons.httpclient.URIException;  
  10. import org.apache.commons.httpclient.methods.GetMethod;  
  11. import org.apache.commons.httpclient.methods.PostMethod;  
  12. import org.apache.commons.httpclient.params.HttpMethodParams;  
  13. import org.apache.commons.httpclient.util.URIUtil;  
  14.   
  15. /** 
  16.  *  
  17.  *  
  18.  * <p>Title:HttpTookitEnhance</p> 
  19.  * <p>Description: httpclient模擬http請求,解決返回內容亂碼問題</p> 
  20.  * <p>Copyright: Copyright (c) 2010</p> 
  21.  * <p>Company: </p> 
  22.  * @author libin 
  23.  * @version 1.0.0 
  24.  */  
  25. public class HttpTookitEnhance  
  26. {  
  27.       /**  
  28.        * 執行一個HTTP GET請求,返回請求響應的HTML  
  29.        *  
  30.        * @param url                 請求的URL地址  
  31.        * @param queryString 請求的查詢參數,可以爲null  
  32.        * @param charset         字符集  
  33.        * @param pretty            是否美化  
  34.        * @return 返回請求響應的HTML  
  35.        */  
  36.       public static String doGet ( String url, String queryString, String charset, boolean pretty )  
  37.       {  
  38.             StringBuffer response = new StringBuffer();  
  39.             HttpClient client = new HttpClient();  
  40.             GetMethodmethod = new GetMethod(url);  
  41.             try  
  42.             {  
  43.                   if ( queryString != null && !queryString.equals("") )  
  44.                         //對get請求參數做了http請求默認編碼,好像沒有任何問題,漢字編碼後,就成爲%式樣的字符串   
  45.                         method.setQueryString(URIUtil.encodeQuery(queryString));  
  46.                   client.executeMethod(method);  
  47.                   if ( method.getStatusCode() == HttpStatus.SC_OK )  
  48.                   {  
  49.                         BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));  
  50.                         String line;  
  51.                         while ( ( line = reader.readLine() ) != null )  
  52.                         {  
  53.                               if ( pretty )  
  54.                                     response.append(line).append(System.getProperty("line.separator"));  
  55.                               else  
  56.                                     response.append(line);  
  57.                         }  
  58.                         reader.close();  
  59.                   }  
  60.             }  
  61.             catch ( URIException e )  
  62.             {  
  63.             }  
  64.             catch ( IOException e )  
  65.             {  
  66.             }  
  67.             finally  
  68.             {  
  69.                   method.releaseConnection();  
  70.             }  
  71.             return response.toString();  
  72.       }  
  73.   
  74.       /**  
  75.        * 執行一個HTTP POST請求,返回請求響應的HTML  
  76.        *  
  77.        * @param url         請求的URL地址  
  78.        * @param params    請求的查詢參數,可以爲null  
  79.        * @param charset 字符集  
  80.        * @param pretty    是否美化  
  81.        * @return 返回請求響應的HTML  
  82.        */  
  83.       public static String doPost ( String url, Map<String, String> params, String charset, boolean pretty )  
  84.       {  
  85.             StringBuffer response = new StringBuffer();  
  86.             HttpClient client = new HttpClient();  
  87.             PostMethodmethod = new PostMethod(url);  
  88.             //設置Http Post數據   
  89.             if ( params != null )  
  90.             {  
  91.                   HttpMethodParams p = new HttpMethodParams();  
  92.                   for ( Map.Entry<String, String> entry : params.entrySet() )  
  93.                   {  
  94.                         p.setParameter(entry.getKey(), entry.getValue());  
  95.                   }  
  96.                   method.setParams(p);  
  97.             }  
  98.             try  
  99.             {  
  100.                   client.executeMethod(method);  
  101.                   if ( method.getStatusCode() == HttpStatus.SC_OK )  
  102.                   {  
  103.                         BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));  
  104.                         String line;  
  105.                         while ( ( line = reader.readLine() ) != null )  
  106.                         {  
  107.                               if ( pretty )  
  108.                                     response.append(line).append(System.getProperty("line.separator"));  
  109.                               else  
  110.                                     response.append(line);  
  111.                         }  
  112.                         reader.close();  
  113.                   }  
  114.             }  
  115.             catch ( IOException e )  
  116.             {  
  117.             }  
  118.             finally  
  119.             {  
  120.                   method.releaseConnection();  
  121.             }  
  122.             return response.toString();  
  123.       }  
  124.   
  125.       public static void main ( String [] args )  
  126.       {  
  127.             String y = doGet("http://video.sina.com.cn/life/tips.html"null"GBK"true);  
  128.             System.out.println(y);  
  129.       }  
  130.   
  131. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章