java類比http get和post 提交

來源:互聯網
上載者:User

使用httpclient工具包commons-httpclient-3.1.jar,依賴commons-logging-1.0.4.jar和commons-codec-1.3.jar。 

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Map;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.URIException;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.commons.httpclient.util.URIUtil;/** *  *  * <p>Title:HttpTookitEnhance</p> * <p>Description: httpclient類比http請求,解決返回內容亂碼問題</p> * <p>Copyright: Copyright (c) 2010</p> * <p>Company: </p> * @author libin * @version 1.0.0 */public class HttpTookitEnhance{      /**        * 執行一個HTTP GET請求,返回請求響應的HTML        *        * @param url                 請求的URL地址        * @param queryString 請求的查詢參數,可以為null        * @param charset         字元集        * @param pretty            是否美化        * @return 返回請求響應的HTML        */      public static String doGet ( String url, String queryString, String charset, boolean pretty )      {            StringBuffer response = new StringBuffer();            HttpClient client = new HttpClient();            HttpMethod method = new GetMethod(url);            try            {                  if ( queryString != null && !queryString.equals("") )                        //對get請求參數做了http請求預設編碼,好像沒有任何問題,漢字編碼後,就成為%式樣的字串                         method.setQueryString(URIUtil.encodeQuery(queryString));                  client.executeMethod(method);                  if ( method.getStatusCode() == HttpStatus.SC_OK )                  {                        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));                        String line;                        while ( ( line = reader.readLine() ) != null )                        {                              if ( pretty )                                    response.append(line).append(System.getProperty("line.separator"));                              else                                    response.append(line);                        }                        reader.close();                  }            }            catch ( URIException e )            {            }            catch ( IOException e )            {            }            finally            {                  method.releaseConnection();            }            return response.toString();      }      /**        * 執行一個HTTP POST請求,返回請求響應的HTML        *        * @param url         請求的URL地址        * @param params    請求的查詢參數,可以為null        * @param charset 字元集        * @param pretty    是否美化        * @return 返回請求響應的HTML        */      public static String doPost ( String url, Map<String, String> params, String charset, boolean pretty )      {            StringBuffer response = new StringBuffer();            HttpClient client = new HttpClient();            HttpMethod method = new PostMethod(url);            //設定Http Post資料             if ( params != null )            {                  HttpMethodParams p = new HttpMethodParams();                  for ( Map.Entry<String, String> entry : params.entrySet() )                  {                        p.setParameter(entry.getKey(), entry.getValue());                  }                  method.setParams(p);            }            try            {                  client.executeMethod(method);                  if ( method.getStatusCode() == HttpStatus.SC_OK )                  {                        BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));                        String line;                        while ( ( line = reader.readLine() ) != null )                        {                              if ( pretty )                                    response.append(line).append(System.getProperty("line.separator"));                              else                                    response.append(line);                        }                        reader.close();                  }            }            catch ( IOException e )            {            }            finally            {                  method.releaseConnection();            }            return response.toString();      }      public static void main ( String [] args )      {            String y = doGet("http://video.sina.com.cn/life/tips.html", null, "GBK", true);            System.out.println(y);      }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.