Java 为什么url中的字符串在发送GET请求后被更改了几个字符?

Java 为什么url中的字符串在发送GET请求后被更改了几个字符?,java,json,servlets,http-get,Java,Json,Servlets,Http Get,背景: 我正在尝试使用,向手机发送短信并接收发送状态。我向他们的服务器发出POST请求。然后,我从收到的JSON响应中提取一个字符串。我使用该字符串向包含该ID的URL发出另一个GET请求。问题是,当我发出GET请求时,该字符串会发生更改。虽然当我从响应中提取它时它是相同的,但是不知道为什么它在GET请求过程中会发生变化 String url2 = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel:7511/req

背景:

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

String url2 = "https://oneapi-gw.gsma.com/smssend/2_0/smsmessaging/outbound/tel:7511/requests/"+id+"/deliveryInfos"
遵循的方法和问题解释:

来自初始
POST
request`的响应:

{"resourceReference":
     {"resourceURL":"https:\/\/oneapi-gw.gsma.com:443\/SendSmsService\/OneAPI_REST_v2_0\/routing\/2_0\/smsmessaging\/outbound\/tel:7511\/requests\/998371119"}
}
我提取在url(998371119)中收到的
ID
。我使用
.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的最后两位数字似乎会自动更改,并且在发出GET请求后,我会以
400
的形式接收
Response code
。以下是错误流:

    {  "requestError" : 
        {    "serviceException" : { 
                 "text" : "Invalid input value for message part requestIdentifier",
                 "variables" : [ 
                     "requestIdentifier", "998371122" 
                  ]    
              }
        }
    }
注意服务器如何将ID解释为
998371122
,而不是
998371119
(我最初请求的ID)。最后两个数字变了。我想了很多,但不知道为什么会这样。虽然当我使用
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" ]
    }
  }
}

编辑:

两天后,这是一个奇怪的解决方案。结果表明,解析很好,
GET
POST
请求的形成也很好。在我收到来自
POST
请求的响应之后,在我发送另一个
GET
请求之前,我只延迟了3秒钟。这就解决了问题

try {
    Thread.sleep(3000);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}

两天后,终于有了一个奇怪的解决办法。结果表明,解析很好,
GET
POST
请求的形成也很好。在我收到来自
POST
请求的响应之后,在我发送另一个
GET
请求之前,我只延迟了3秒钟。这就解决了问题

try {
    Thread.sleep(3000);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}

你有没有试过用一种工具来检查电线上到底发生了什么?@河马:这是个好主意。我试试那个工具,看看发生了什么事。谢谢,不客气。告诉我们进展如何。@河马:我已经发布了日志。所有的GET/POST请求都进行得很顺利。这似乎是服务器端的问题。我摆出适合我的姿势。谢谢你的帮助:)你有没有试着用一种工具来检查电线上到底发生了什么?@Hippo:这是个好主意。我试试那个工具,看看发生了什么事。谢谢,不客气。告诉我们进展如何。@河马:我已经发布了日志。所有的GET/POST请求都进行得很顺利。这似乎是服务器端的问题。我摆出适合我的姿势。谢谢你的帮助:)