HttpClient基本用法

来源:互联网 发布:凤凰刷机软件 编辑:程序博客网 时间:2024/06/06 08:58

Get方式:

String url="http://localhost:8080/HttpClientDemo/test";HttpGet httpRequest=new HttpGet(url);HttpClient httpClient=new DefaultHttpClient();HttpResponse response=httpClient.execute(httpRequest);if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){String result=EntityUtils.toString(response.getEntity());System.out.println(result);}


Post方式:

String url="http://localhost:8080/HttpClientDemo/test";HttpPost request=new HttpPost(url);List<NameValuePair> params=new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("name", "testname"));HttpEntity httpEntity=new UrlEncodedFormEntity(params,"utf-8");request.setEntity(httpEntity);HttpClient httpClient=new DefaultHttpClient();HttpResponse response=httpClient.execute(request);if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){String result=EntityUtils.toString(response.getEntity());System.out.println(result);}


原创粉丝点击