博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient基本用法
阅读量:6671 次
发布时间:2019-06-25

本文共 1013 字,大约阅读时间需要 3 分钟。

hot3.png

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/joeytai/blog/521127

你可能感兴趣的文章