无法通过REST调用获取json响应,但CURL获得响应

时间:2018-06-13 13:10:59

标签: java json rest curl

这是我发送请求的curl命令和json响应。

 curl --verbose \
>   -X POST \
>   -H "Content-Type: application/json" \
>   -H "ABC-Reply-Wait-Time-In-ms: 30000" \
>   --data '{"mobileNo":"xxxxxxxxxx","message":"Your One-Time Password code is {OTP} (OTP Ref = {REF}) {SMSTIME}"}' \
>   --basic --user username:Password \ {URL}

* Server auth using Basic with user 'username'
> POST /TOPIC/abc_PT_GTY/otpRequestQ HTTP/1.1
> Authorization: Basic zxfsvdcbchdbdbcbc==
> User-Agent: curl/2.90.0
> Host: abcd:9999
> Accept: */*
> Content-Type: application/json
> ABC-Reply-Wait-Time-In-ms: 30000
> Content-Length: 102
>
* upload completely sent off: 102 out of 102 bytes
< HTTP/1.1 200 OK
< Cache-Control: no-cache
< Content-Length: 57
< Content-Type: application/json
< Server: abc/3.9.0.28
< Set-Cookie: qasw=wertyuie492ef90d3; Path=/
< ABC-Client-Name: #rest/client/10.201.93.21/67542/wertyuie492ef90d3
< ABC-Message-ID: ID:10.128.196.7865b7765f3a1d2gb0:25
< ABC-Timestamp: 12345678987654
< ABC-User-Property-breadcrumbId: topic_abc_PT_GTY_otpRequestQ_null
< ABC-User-Property-CamelJmsDeliveryMode: 2; type=int32
<
* Connection #0 to host abcd left intact
{"status":"success","message":"OTP Sent","otpRef":"QAKQ"}[abcdev@bdslideltsop005 usr]$

休息服务方法是

private StringBuilder getServiceResponse(String request, HttpURLConnection con)
            throws IOException, UnsupportedEncodingException {
        OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
        wr.write(request);
        wr.flush();

        StringBuilder sb = new StringBuilder();
        int HttpResult = con.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();
            logger.info("response from generate.otp.service: " + sb.toString());
            System.out.println("" + sb.toString());
        } else {
            logger.info("response from generate.otp.service: " + sb.toString());
            sb.append(con.getResponseMessage());
        }
        return sb;
    }

private HttpURLConnection createConnection(String url) throws MalformedURLException, IOException, ProtocolException {
        URL object = new URL(url);
        HttpURLConnection con = (HttpURLConnection) object.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Authorization", "basic"+ encoding);
        con.setRequestMethod("POST");
        return con;
    }

这是短信服务,当使用curl命令时,如上所示获得正确的响应,但是使用来自代码的REST服务调用,虽然在移动设备上获取短信,但是没有获得Json响应....

请帮忙排序......先谢谢!!!!

0 个答案:

没有答案