zoukankan      html  css  js  c++  java
  • 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. }  
  • 相关阅读:
    HDU 4053 or ZOJ 3541 The Last Puzzle【区间dp】【经典题】
    HDU 4053 or ZOJ 3541 The Last Puzzle【区间dp】【经典题】
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) 831C. Jury Marks【二分】【水题】
    Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) 831C. Jury Marks【二分】【水题】
    POJ 2955 Brackets 【区间dp】【水题】
    POJ 2955 Brackets 【区间dp】【水题】
    动手动脑2
    统计创建了多少类
    动手动脑1
    动手动脑
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3167874.html
Copyright © 2011-2022 走看看