码迷,mamicode.com
首页 > 其他好文 > 详细

接口post包的方式传输

时间:2017-08-29 16:49:55      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:java   seh   turn   stack   cut   null   encoding   throw   div   

   public static String doPostJson(String url, String param) {
		String responseBody = "";
		HttpPost httppost = new HttpPost(url);
		StringEntity entities = new StringEntity(param, "UTF-8");
		entities.setContentEncoding("UTF-8");
		entities.setContentType("application/x-www-form-urlencoded");
		httppost.setEntity(entities);
		ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

			public String handleResponse(final HttpResponse response)
					throws ClientProtocolException, IOException {
				int status = response.getStatusLine().getStatusCode();
				if (status >= 200 && status < 300) {
					HttpEntity entity = response.getEntity();
					return entity != null ? EntityUtils.toString(entity)
							: null;
				} else {
					throw new ClientProtocolException(
							"Unexpected response status: " + status);
				}
			}
		};
		try {
			responseBody = new DefaultHttpClient().execute(httppost, responseHandler);
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
			responseBody = "timeout";
			return responseBody;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return responseBody;
		} catch (IOException e) {
			e.printStackTrace();
			return responseBody;
		}
		return responseBody;
	}

  

接口post包的方式传输

标签:java   seh   turn   stack   cut   null   encoding   throw   div   

原文地址:http://www.cnblogs.com/cuijinlong/p/7449045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!