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);
}