美文网首页
httpClient的patch请求代码实现

httpClient的patch请求代码实现

作者: 大力水手三号 | 来源:发表于2017-03-10 22:55 被阅读0次

    @SuppressWarnings("unused")

    public static JSONObject httpPatch(String Authorization, JSONObject jsonParam, String url) {

    JSONObject resultObj = null;

    String strResult = "";

    @SuppressWarnings("deprecation")

    HttpClient httpClient = new DefaultHttpClient();

    HttpPatch httpPatch = new HttpPatch(url);

    httpPatch.setHeader("Content-type", "application/json");

    httpPatch.setHeader("Charset", HTTP.UTF_8);

    httpPatch.setHeader("Accept", "application/json");

    httpPatch.setHeader("Accept-Charset", HTTP.UTF_8);

    httpPatch.setHeader("Authorization", Authorization);

    if (jsonParam == null) {

    return resultObj; 

    }

    try {

    StringEntity entity = new StringEntity(jsonParam.toString(), HTTP.UTF_8);

    System.out.println("entity:" + entity.toString());

    httpPatch.setEntity(entity);

    HttpResponse response = httpClient.execute(httpPatch);

    int statusCode = response.getStatusLine().getStatusCode();

    switch (statusCode) {

    case HttpStatus.SC_OK:

    /**

    * 读取服务器返回过来的json字符串数据*

    */

    strResult = EntityUtils.toString(response.getEntity());

    System.out.println("strResult:" + strResult);

    resultObj = JSONObject.fromObject(strResult);

    break;

    case HttpStatus.SC_METHOD_FAILURE:

    // 其他一些处理

    resultObj = null;

    break;

    // ..........还有好多状态,可以按照需要处理

    default:

    resultObj = null;

    break;

    }

    } catch (ParseException | JSONException | IOException e) {

    e.printStackTrace();

    return null;

    }

    return resultObj;

    }

    相关文章

      网友评论

          本文标题:httpClient的patch请求代码实现

          本文链接:https://www.haomeiwen.com/subject/kgnogttx.html