java httpurlconnection post 参数_Java的http post请求01之HttpURLConnection

packagecom.ricoh.rapp.ezcx.iwbservice.webservice;importjava.io.BufferedOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.SocketException;importjava.net.SocketTimeoutException;importjava.net.URL;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.ricoh.rapp.ezcx.iwbservice.util.Constant;importcom.ricoh.rapp.ezcx.iwbservice.util.Utils;public classHttpRequest {private final Logger logger = LoggerFactory.getLogger(HttpRequest.class);/*** http请求

*

*@paramurlPath

* 请求路径http://127.0.0.1:8080/index

*@paramrequestStr

* 请求参数

**/

publicJSONObject execute(String urlPath, String requestStr) {

HttpURLConnection httpConnection= null;try{

URL url= newURL(urlPath);

httpConnection=(HttpURLConnection) url.openConnection();

httpConnection.setRequestMethod("POST");

httpConnection.setDoOutput(true); //post请求为true;get请求为false并且不用out写入数据

httpConnection.setDoInput(true); //有相应数据

httpConnection.setConnectTimeout("3 * 1000");

httpConnection.setReadTimeout("Charset");//set http header

httpConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");

httpConnection.setRequestProperty("Charset", "UTF-8");

httpConnection.connect();//send data

OutputStream buf =httpConnection.getOutputStream();

buf= newBufferedOutputStream(buf);

OutputStreamWriter out= newOutputStreamWriter(buf);if (requestStr == null || requestStr.length() == 0) {

requestStr= newJSONObject().toString();

}

out.write(requestStr);

out.flush();

out.close();//receive data

InputStream inputStream =httpConnection.getInputStream();if (httpConnection.getResponseCode() !=HttpURLConnection.HTTP_OK) {

logger.error("connect ezcs service failed: " +httpConnection.getResponseCode());

JSONObject responseJson= newJSONObject();

responseJson.put(ResponseKey.KEY_RESULT,

com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);returnresponseJson;

}

String response=Utils.convertStreamToString(inputStream, Constant.ENCODING_UTF_8);

logger.debug("response from ezcs service: " +response);

JSONObject responseJson=JSON.parseObject(response);returnresponseJson;

}catch(Exception e) {

e.printStackTrace();

logger.debug("connect local ezcs service exception: " +e.getMessage());

JSONObject responseJson= newJSONObject();if (e instanceof SocketTimeoutException || e instanceofSocketException) {

responseJson.put(ResponseKey.KEY_RESULT,

com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_SERVER_HTTP_ERROR);

}else{

responseJson.put(ResponseKey.KEY_RESULT,

com.ricoh.rapp.unifiedPlatform.dsdkService.constant.HttpConstant.ResultCode.ERROR_INNER_ERROR);

}returnresponseJson;

}finally{if (httpConnection != null) {

httpConnection.disconnect();

}

}

}public static classResponseKey {public static final String KEY_RESULT = "result";public static final String KEY_REASON = "reason";public static final String KEY_DATA = "data";public static final String KEY_EXTRA = "extra";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值