首页 > 其他 > 详细

接口post包的方式传输

时间:2017-08-29 16:45:45      阅读:215      评论:0      收藏:0      [点我收藏+]
   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包的方式传输

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!