Java HttpClient 的簡單應用

new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                
                DefaultHttpClient client = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost(
                        "www.baidu.com");

                List<NameValuePair> parameters = new ArrayList<NameValuePair>();
                parameters.add(new BasicNameValuePair("key", value));
                parameters.add(new BasicNameValuePair("key", value));

                UrlEncodedFormEntity entity;
                try {
                    entity = new UrlEncodedFormEntity(parameters, "utf-8");
                    httpPost.setEntity(entity);
                    HttpResponse response = client.execute(httpPost);
                    HttpEntity resultEntity = response.getEntity();
                    String body = EntityUtils.toString(resultEntity);
                    
                  System.out.println(body)

                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("---UnsupportedEncodingException--");
                 
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                 
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    System.out.println("---IOException--");
                 
                }
            }
        }).start();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章