Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将JSON作为HTTP参数传递_Java_Json - Fatal编程技术网

Java 无法将JSON作为HTTP参数传递

Java 无法将JSON作为HTTP参数传递,java,json,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-Agen

我正在使用一个开源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 
我还需要将密码提交到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();
    }

}

您可以添加else语句并打印状态吗?如果(statusCode==“201”){System.out.println(“IP现在在列表中”);}您真的打算发布登录凭据吗?它似乎是一个测试服务器,但仍然…@Henry它在我的测试vmware中,所以对密码来说并不重要。您可以添加else语句并打印状态吗?如果(statusCode==“201”){System.out.println(“IP现在在列表中”);}您真的打算发布登录凭据吗?它似乎是一个测试服务器,但仍然…@Henry它在我的测试vmware中,所以密码并不重要
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();
    }

}