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); Listparams=new ArrayList (); 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); }
版权声明:本文为博主原创文章,未经博主允许不得转载。