1

背景:

我正在尝试使用one-api向手机发送短信并接收交付状态。我POST向他们的服务器发出请求。JSON然后我从收到的响应中提取一个字符串。我使用该字符串向GET包含该 ID 的 URL 发出另一个请求。问题是当我提出GET请求时字符串会发生变化。虽然我从响应中提取它时是一样的,但不知道为什么它在 GET 请求过程中会发生变化。

遵循的方法和问题解释:

初始请求的响应POST`:

{"resourceReference":
     {"resourceURL":"https:\/\/oneapi-gw.gsma.com:443\/SendSmsService\/OneAPI_REST_v2_0\/routing\/2_0\/smsmessaging\/outbound\/tel:7511\/requests\/998371119"}
}

我提取了ID我在 url (998371119) 中收到的内容。我.split用来提取如下:

String tmp = (String)resourceReference.get("resourceURL");
String [] tmp2 = tmp.split("/");
String id = tmp2[(tmp2.length)-1].toString(); 
System.out.println(id); 
// the output is:998371119.

使用此 ID 创建另一个 url 来发送 GET 请求。

String url2 = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel:7511/requests/"+id+"/deliveryInfos"

当我发送GET请求时,ID 的最后 2 位数字似乎会自动更改,并且在发出 GET 请求后我会收到Response Codeas 。400以下是错误流:

    {  "requestError" : 
        {    "serviceException" : { 
                 "text" : "Invalid input value for message part requestIdentifier",
                 "variables" : [ 
                     "requestIdentifier", "998371122" 
                  ]    
              }
        }
    }

请注意服务器如何将 ID 解释为998371122998371119我最初请求的)。最后 2 位数字变了。我想了很多,但我不知道为什么会这样。虽然当我curl用来发送GET请求时,一切正常。所以不是服务器问题。他们在我如何形成 url 或提取 ID 或发出 GET 请求方面存在一些问题。有什么建议么?提前致谢。

您可能需要的其他相关代码

这就是我提出GET请求的方式:

private String getResponseFromGETRequest(String accept, String url) {

    URL obj;
    StringBuffer response = new StringBuffer();;
    HttpURLConnection con;
    String authHeaderValue = new String(Base64.encode(credentials.getBytes()));
    //Credentals variable is a string storing "username:password"
    try {
        obj = new URL(url);
        con = (HttpURLConnection) obj.openConnection();
        con.setRequestProperty ("Authorization", "Basic " + authHeaderValue);

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty ("Accept", accept);
        //con.setRequestProperty("Content-Type", accept);

        int responseCode = con.getResponseCode();
        InputStream ipStream;
        if (con.getResponseCode() >= 400) {
            ipStream = con.getErrorStream();
        } else {
            ipStream = con.getInputStream();
        }

        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(ipStream));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }

    return response.toString();
}

日志猫:

Oct 04, 2013 3:59:07 PM sun.net.www.protocol.http.HttpURLConnection plainConnect
FINEST: ProxySelector Request for https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel%3A7511/requests
Oct 04, 2013 3:59:07 PM sun.net.www.protocol.http.HttpURLConnection plainConnect
FINEST: Proxy used: DIRECT
Oct 04, 2013 3:59:08 PM sun.net.www.protocol.http.HttpURLConnection writeRequests
FINE: sun.net.www.MessageHeader@7f47e35410 pairs: {POST /smssend/2_0/smsmessaging/outbound/tel%3A7511/requests HTTP/1.1: null}{Authorization: Basic ABCDEFGHMYAUTHORIZATIONKEY=}{Accept: application/json}{Content-Type: application/json}{Cache-Control: no-cache}{Pragma: no-cache}{User-Agent: Java/1.7.0_21}{Host: oneapi-gw.gsma.com}{Connection: keep-alive}{Content-Length: 212}
Oct 04, 2013 3:59:08 PM sun.net.www.protocol.http.HttpURLConnection getInputStream
FINE: sun.net.www.MessageHeader@75240d4a17 pairs: {null: HTTP/1.1 201 Created}{Date: Fri, 04 Oct 2013 19:59:08 GMT}{Server: Jetty(6.1.x)}{Content-Type: application/json}{Location: https://oneapi-gw.gsma.com:443/SendSmsService/OneAPI_REST_v2_0/routing/2_0/smsmessaging/outbound/tel:7511/requests/998380556}{Host: oneapi-gw.gsma.com}{X-Forwarded-Server: oneapi-gw.gsma.com}{Authorization: Basic ABCDEFGHMYAUTHORIZATIONKEY=}{User-Agent: Java/1.7.0_21}{Accept: application/json}{X-Forwarded-For: 10.90.24.132}{X-Forwarded-Host: oneapi-gw.gsma.com}{breadcrumbId: ID-dtx-prod-apihr01-39903-1371168975552-0-440221}{Vary: Accept-Encoding,User-Agent}{Keep-Alive: timeout=5, max=100}{Connection: Keep-Alive}{Transfer-Encoding: chunked}

Oct 04, 2013 3:59:08 PM sun.net.www.protocol.http.HttpURLConnection plainConnect
FINEST: ProxySelector Request for https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel:7511/requests/998380556/deliveryInfos
Oct 04, 2013 3:59:08 PM sun.net.www.protocol.http.HttpURLConnection plainConnect
FINEST: Proxy used: DIRECT
Oct 04, 2013 3:59:08 PM sun.net.www.protocol.http.HttpURLConnection writeRequests
FINE: sun.net.www.MessageHeader@71d198cb9 pairs: {GET /smssend/2_0/smsmessaging/outbound/tel:7511/requests/998380556/deliveryInfos HTTP/1.1: null}{Authorization: Basic ABCDEFGHMYAUTHORIZATIONKEY=}{Accept: application/json}{Content-Type: application/json}{Cache-Control: no-cache}{Pragma: no-cache}{User-Agent: Java/1.7.0_21}{Host: oneapi-gw.gsma.com}{Connection: keep-alive}
Oct 04, 2013 3:59:08 PM sun.net.www.protocol.http.HttpURLConnection getInputStream
FINE: sun.net.www.MessageHeader@778671cd15 pairs: {null: HTTP/1.1 400 Bad Request}{Date: Fri, 04 Oct 2013 19:59:09 GMT}{Server: Jetty(6.1.x)}{Content-Type: application/json}{Accept: application/json}{Host: oneapi-gw.gsma.com}{breadcrumbId: ID-dtx-prod-apihr02-48223-1371168511818-0-440001}{X-Forwarded-Host: oneapi-gw.gsma.com}{X-Forwarded-For: 10.90.24.132}{User-Agent: Java/1.7.0_21}{X-Forwarded-Server: oneapi-gw.gsma.com}{Authorization: Basic ABCDEFGHMYAUTHORIZATIONKEY=}{Vary: Accept-Encoding,User-Agent}{Connection: close}{Transfer-Encoding: chunked}
Header: Date : Fri, 04 Oct 2013 19:59:09 GMT
Header: Server : Jetty(6.1.x)
Header: Content-Type : application/json
Header: Accept : application/json
Header: Host : oneapi-gw.gsma.com
Header: breadcrumbId : ID-dtx-prod-apihr02-48223-1371168511818-0-440001
Header: X-Forwarded-Host : oneapi-gw.gsma.com
Header: X-Forwarded-For : 10.90.24.132
Header: User-Agent : Java/1.7.0_21
Header: X-Forwarded-Server : oneapi-gw.gsma.com
Header: Authorization : Basic ABCDEFGHMYAUTHORIZATIONKEY=
Header: Vary : Accept-Encoding,User-Agent
Header: Connection : close
Header: Transfer-Encoding : chunked
Response Code : 400
{
  "requestError" : {
    "serviceException" : {
      "messageId" : "SVC0002",
      "text" : "Invalid input value for message part requestIdentifier",
      "variables" : [ "requestIdentifier", "998380559" ]
    }
  }
}

编辑:

4

1 回答 1

1

最后两天后,这是一个奇怪的解决方案。事实证明解析很好,GET请求POST的形成也很好。POST在收到请求的响应后和发送另一个请求之前,我只延迟了 3 秒GET。这解决了它。

try {
    Thread.sleep(3000);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
于 2013-10-04T19:36:34.023 回答