`

HttpClient通过entity上传数据

 
阅读更多

 

HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(url);
		byte[] b =json.toString().getBytes();
		RequestEntity entity = new ByteArrayRequestEntity(b);
		post.setRequestEntity(entity);
		post.setRequestHeader("Content-Type", "application/octet-stream");
		int code = client.executeMethod(post);
		String result=post.getResponseBodyAsString();

 或者

DefaultHttpClient httpClient=new DefaultHttpClient();
		HttpEntity httpEntity = new StringEntity(iq.toXML(), "UTF-8");
		HttpPost httpPost = new HttpPost("http://******:7080/channel/send");
		httpPost.setEntity(httpEntity);
		HttpResponse response = httpClient.execute(httpPost);
		InputStream is = response.getEntity().getContent();

		BufferedReader in = new BufferedReader(new InputStreamReader(is));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		while ((line = in.readLine()) != null) {
			buffer.append(line);
		}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics