JSONException:在HTTPS POST响应中,JSONObject文本必须以字符1错误中的“{”开头

时间:2017-10-02 07:03:07

标签: java json restful-authentication connectivity

我正在尝试将HTTPS请求发送到客户端的某个服务器。反过来,从服务器返回的响应是missing "{" from the JSON response.

当客户端正在测试时,他们会收到正确的响应。

请你帮我解决一下代码内容的问题。同样的问题仍然缺少“<”中的人物 将标题更改为“Accept”,“application / xml”时的XML响应。

 // Request headers that needs to be passed

     public Object generateHttpHeader(String requestType, IYodRobot pRobot, Object request) {       

            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Connection"," Keep-Alive");
            headers.put("Content-type", "application/x-www-form-urlencoded");
            headers.put("Accept", "application/json");
            headers.put("Accept-Language", "en-US");
            headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)");
            headers.put("Accept-Encoding", "identity");
            headers.put("Cookie", "Aggregator=stuff");
     // Headers generated that need to be passed.

            return headers; 
        }
     // Posting request 
     protected String postRequestLocal(Object generateHttpHeader){      
     URL url;
            HttpsURLConnection urlConn;
            //Generating HTTPS connection object
            OutputStreamWriter wout = null;
            BufferedReader br = null;
            StringBuffer ResponseData = new StringBuffer();
            InputStreamReader isr = null;
            String contents = "";
            String reasonForRetry = "";
            String URL = "https://XXXXXXX/service/Servlet";
            //Constructing request
            String request = "UID=" + "abcdefgh" + "&KEY=" + "12344";

            System.out.println("2^^^^^^^ URL is: " + URL);
            System.out.println("Request is ::"+request);

            try {
               //URL connection creation
                url = new URL(URL);
                urlConn = (javax.net.ssl.HttpsURLConnection)
                url.openConnection();


                urlConn.setRequestMethod("POST");
                urlConn.setInstanceFollowRedirects(false);


                String  clientKeyStore = "XXXXXXXX";
                String  password = "XXXXXX";
                // 2- WAY SSL Connectivity 
                urlConn.setSSLSocketFactory(getFactory(new File("/XXX/XXX/XX/XXX/" + clientKeyStore), password));

                urlConn.setDoInput(true);
                urlConn.setDoOutput(true);
                urlConn.connect();

                HashMap<String, String> headers = (HashMap<String, String>) generateHttpHeader;

                System.out.println("2^^^^^^^^^^^^^printing url connection " + urlConn.toString());

                for (Iterator<Map.Entry<String, String>> headerIter = headers.entrySet().iterator(); 

     headerIter.hasNext();) {
                    System.out.println(" In here.......................");
                    Map.Entry<String, String> entry = headerIter.next();
                    urlConn.setRequestProperty(entry.getKey(), entry.getValue());
                    System.out.println("2^^^^^^^ printing key " + entry.getKey().toString() + "  value is" + 

     entry.getValue().toString());
                }
     //Hashmap for request headers that needs to be passed`enter code here`
                Map<String, List<String>> requestHeaders = urlConn.getRequestProperties();
                YDataLogger.out("2^^^^^^^^^^Request Header Fields is: "+ requestHeaders);
                YDataLogger.out("urlConn.getRequestMethod"+urlConn.getRequestMethod());



                    wout = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
                    System.out.println("2^^^^^^^^^checkpoint 8");
                    wout.write(request);
                    System.out.println("2^^^^^^^^^wout" + wout.toString());
                    wout.flush();
                    wout.close();

                /*
                 * Getting the soap error message if we have error.
                 */
                System.out.println("2^^^ Trying to print the fault");
                System.out.println("2^^^^^^ urlConngetResponseCode--->" + urlConn.getResponseCode());
                System.out.println("2^^^^^^ urlConngetResponseMessage--->" + urlConn.getResponseMessage());

                if (urlConn.getResponseCode() == 200) {
                    isr = new InputStreamReader(urlConn.getInputStream());              
                } else if(urlConn.getResponseCode() == 500) {
                    throw new SiteApplicationErrorException("Server down", urlConn.getResponseMessage());
                }
                    else {
                    isr = new InputStreamReader(urlConn.getErrorStream());
                }

                Map<String, List<String>> responseHeaders = urlConn.getHeaderFields();
                System.out.println("2^^^^^^^^^^Response Header Fields is: "+responseHeaders);

                System.out.println("2^^^^^^^ response header---"+urlConn.getHeaderFields());


                    br = new BufferedReader(isr);

                String s = null;
                while ((s = br.readLine()) != null) {
                    ResponseData.append(s);
                }
     //Diconnecting URL connecttion


                urlConn.disconnect();
                /*
                 * getting the soap Data/Message if we have error.
                 */

                Date end = new Date();
                System.out.println("2^^^ Properties" + urlConn.getRequestProperties());
                System.out.println("2^^^^^" + end.toLocaleString());
                contents = ResponseData.toString();



            } 
 //catching exception
 catch (Exception ex) {
                System.out.println("2^^^^^ getStackTrace-->\n" + getStackTrace(ex));
                reasonForRetry = ex.getMessage();

在https失败的情况下打印堆栈跟踪

                if (ex.getMessage().contains(
                        "server returned http response code: 500")) {
                    throw new SiteApplicationErrorException(
                            "2^^^^Top Level Error - Returned HTTP response code: 500 :: Exact error"
                                    + ex.getMessage());
                } else {
                    System.out.println("2^^^^^ getStackTrace-->\n" + getStackTrace(ex));
                    throw new GeneralException(ex.toString() + "\n"
                            + ex.getMessage());
                }

            } finally {
                if (wout != null) {
                    wout.close();
                }
                if (isr != null) {
                    isr.close();
                }
            }
     // Printing the response returned back
            System.out.println("2^^^^^^^^^Contents  : "+contents);
     }

1 个答案:

答案 0 :(得分:0)

我的猜测是webservice期望参数UID和KEY作为JSON(或XML)传递,而不是application / x-www-form-urlencoded。