1. 程式人生 > >httpclient簡單例項demo

httpclient簡單例項demo

使用jar包httpcore-4.0.jar

private static String httpClent(String url,String con){

        String response="";    
        //建立HttpClient物件
        HttpClient httpClient = new HttpClient();
        HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager()
            .getParams();
        // 設定連線超時時間(單位毫秒) 
        managerParams.setConnectionTimeout(70000);
        // 設定讀資料超時時間(單位毫秒) 
        managerParams.setSoTimeout(70000);
        PostMethod method = null;
        try {       
        method = new PostMethod(url);
            NameValuePair nameValuePair = new NameValuePair("con", con);
            method.setRequestBody(new NameValuePair[] {nameValuePair});     
            httpClient.executeMethod(method);
            response = method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            method.releaseConnection();
        }    
        return response;
    }