无法将JSON作为HTTP参数

时间:2017-06-13 09:21:01

标签: java json

我正在使用一个开源REST API客户端程序使用POST方法对我的远程服务器内的应用程序进行Web服务调用,并且它已成功完成,JSON字符串能够将其传递给服务器,所有必需的信息都是更新,这是来自开源程序的跟踪。

  POST /api/v1.4/loginRestrictionPolicies/blocked/rules HTTP/1.1
> Host: 192.168.0.127:444
> Authorization: Basic YWRtaW46YWRtaW4=
> User-Agent: insomnia/5.2.0
> Accept: */*
> Accept-Encoding: deflate, gzip
> Content-Type: application/json
> Content-Length: 131
**| {
|   "description": "mm",
|   "name": "nn",
|   "enabled": "true",
|   "type": "ALLOW",
|   "clientAddress": "hug",
|   "expression": "mmam" 
| }**
* upload completely sent off: 131 out of 131 bytes
< HTTP/1.1 201 Created
< Server: Apache-Coyote/1.1
< Set-Cookie: 
< Location: https://192.168.0.127:444/api/v1.4/loginRestrictionPolicies/blocked/rules/nn
< X-Login-Restriction-Policy-Name: blocked
< X-Login-Restriction-Rule-Name: nn
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Tue, 13 Jun 2017 06:58:05 GMT 

但是,当我尝试使用我的程序以同样的方式执行并且在我的远程服务器中声明的应用程序中没有进行任何更新时,我的程序没有显示任何错误消息,所以我不知道在哪里我需要解决它,因为程序没有任何异常抛出。
JSON数据将如下所示

{
"description": "mm",
 "name": "nn",
 "enabled": "true",
 "type": "ALLOW",
 "clientAddress": "hug",
 "expression": "mmam" 
}

我还需要将密码提交到Web服务调用的POST请求中。这是我的Web服务调用的源代码,我使用的是Httpclient 4.5.2

public static void PostCall(String t,String rule1)
{
    int TIME_OUT = 1000;
    try 
    {
      System.out.println("in the PostCall method now :" +t);
      JSONObject jo1 = new JSONObject();
      jo1.put("name",rule1);
      jo1.put("enabled","true");
      jo1.put("type","DENY");
      jo1.put("clientAddress",t);
      jo1.put("expression","This IP violated the rule");
      jo1.put("description","You are in my list");  

      System.out.println("The JSON will look like :" +jo1.toString());    
      System.setProperty("javax.net.ssl.trustStore","/usr/lib/java/jdk1.8.0_131/jre/lib/security/cacerts");   
      SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null,new TrustSelfSignedStrategy()).build();
      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1.2" }, null, new oopHostnameVerifier());

      HttpClient client1 = HttpClients.custom().setSSLSocketFactory(sslsf).build();
      HttpPost httpPost = new HttpPost("https://192.168.0.127:444/api/v1.4/loginRestrictionPolicies/blocked/rules");
      RequestConfig defaultRequestConfig = RequestConfig
          .custom()
          .setCookieSpec(CookieSpecs.DEFAULT)
          .setExpectContinueEnabled(true)
          .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
          .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();

      RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
        .setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT)
        .setConnectionRequestTimeout(TIME_OUT).build();

      httpPost.setConfig(requestConfig);
      StringEntity entity1 = new StringEntity(jo1.toString());
      httpPost.addHeader("Accept","application/json");
      httpPost.addHeader("Content-Type","application/json");
      String cre1  = Base64.getEncoder().encodeToString(("admin:admin").getBytes());
      httpPost.addHeader("Authorization","Basic" +cre1);
      httpPost.setEntity(entity1);
      HttpResponse res1 = client1.execute(httpPost);
      BufferedReader rd1 = new BufferedReader(new InputStreamReader(res1.getEntity().getContent()));
      String statusCode = null;

      while((statusCode=rd1.readLine())!=null)
      {
        if(statusCode == "201")
        {
              System.out.println("The IP is in the list now");
        }
      } 

    }
    catch(JSONException e)
    {
      e.printStackTrace();
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

}

0 个答案:

没有答案