帖子请求的回复格式不正确

时间:2019-12-05 13:13:41

标签: java http parsing httpresponse

所以我正在实现一个通用的HTTP post方法,但是它给我的响应与非通用的post方法不同。例如,这是我的通用发布方法。

    public static ResponseEntity<String> sendPOST(String url, String requestBody) throws IOException, URISyntaxException {
     RestTemplate restTemplate = new RestTemplate();
        final String baseUrl = url;
        URI uri = new URI(baseUrl);
        String request = requestBody;  

        HttpEntity<String> request2 = new HttpEntity<String>(request, utilityClass.nonProfitHeaders); 
        ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST,request2, String.class);
        String responseBody = result.getBody();
    return result;
}

我的回复看起来像

<200,<?xml version='1.0' encoding='UTF-8'?><findNonprofitResponse xmlns="http://www.ebay.com/marketplace/fundraising/v1/services"><ack>Success</ack><version>1.0.0</version><timestamp>2019-12-05T13:02:20.236Z</timestamp><nonprofit><name>RSPCA</name><mission>The RSPCA is the leading UK animal welfare charity. We promote kindness and aim to prevent cruelty. 

We rescue, rehabilitate and rehome animals in need. We stand up to those who deliberately harm animals - we will not tolerate animal abuse..</mission><logoURL>https://i.ebayimg.com/00/s/ODQ2WDEzOTY=/z/IAwAAOSwhktdXlni/$_1.JPG?set_id=8800005007</logoURL><nonprofitId>16187</nonprofitId><description>Donate £2 to help rescue, rehabilitate and rehome animals in need</description><externalId>10484</externalId><homePageURL>http://www.ebay.co.uk/charity/charity.jsp?NP_ID=10484</homePageURL><address><latitude>51.037</latitude><longitude>-0.3441</longitude><zipCode>RH13 9RS</zipCode><addressLine1>Wilberforce Way</addressLine1><city>Horsham</city><state>West Sussex</state></address><ein>219099</ein><largeLogoURL>https://i.ebayimg.com/00/s/ODQ2WDEzOTY=/z/IAwAAOSwhktdXlni/$_1.JPG?set_id=8800005007</largeLogoURL></nonprofit><paginationOutput><pageNumber>1</pageNumber><pageSize>25</pageSize><totalEntries>1</totalEntries><totalPages>1</totalPages></paginationOutput></findNonprofitResponse>,[Content-Type:"text/xml;charset=UTF-8", GUID:"16ed6275-fc10-aadc-c607-36478f9f2430", Server:"Apache-Coyote/1.1", X-EBAY-REQUEST-ID:"16ed6275-fc10-aadc-c607-36478f9f2430!services.fundraising.FundRaisingFindingService.v1!10.173.204.96!esbnewexsoaesb[", X-EBAY-SOA-GLOBAL-ID:"EBAY-GB", X-EBAY-SOA-MESSAGE-PROTOCOL:"NONE", X-EBAY-SOA-OPERATION-NAME:"findNonprofit", X-EBAY-SOA-REQUEST-ID:"16ed6275-fba0-aad5-a561-2831fc84208c!FundRaisingFindingServiceV1!10.173.90.86!v3fundraisingcore[]", X-EBAY-SOA-RESPONSE-DATA-FORMAT:"XML", X-EBAY-SOA-SERVICE-METRICS:"5261716000", X-EBAY-SOA-SERVICE-NAME:"{http://www.ebay.com/marketplace/fundraising/v1/services}FundRaisingFindingService", X-EBAY-SOA-SERVICE-VERSION:"1.0.0", Date:"Thu, 05 Dec 2019 13:02:19 GMT", Transfer-Encoding:"chunked", Connection:"keep-alive"]>

响应与我的普通帖子请求之间有一个奇怪的间隙,

    public String getItem(@QueryParam("input")String input) throws IOException {
    System.out.println("Get Item Method");
    String requestBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + 
            "<GetItemRequest xmlns=\"urn:ebay:apis:eBLBaseComponents\">\r\n" + 
            "  <RequesterCredentials>\r\n" + 
            "    <eBayAuthToken>AgAAAA**AQAAAA**aAAAAA**9enaXQ**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6AGmICpDZmDpwWdj6x9nY+seQ**OSoGAA**AAMAAA**mc4q0e94Onut08iyCeBQDC6u1QQokVs4kukRMxe2PSdQ4CsKUIxSGhgFHFraUV0VJ1fLgYmKg6kXDQh0idHTgKjcHFPcuPDmtwT5TSH+SAKHw6TaZkAgVLgOMBprmwgLAJNiTESHarSfuuJxUdILw3lB/rrGC3tQ3sFz8l3SxpCx6NXvuOQ0++3aZ2gB/EPeiT70bR/z87bVkZYoK/OAG/vRCBVch5xo2PiJWJl5xLYpG3iz6aauTiJur3TKC3cPriMLWDkLAOqhXZol9jp2cknPesRfDypOBzDnvECNP81F18t0/3u/Lgn9BWxYdefCm95Rkw3XjZwQTG1GVLqBHoWBpG3s8eLuQhChlbH52ecF7sFb3aSXdvTcOCSwVHMA0GRjPhcoNt91WOfI22tUaJJ7/H72IqosJw235lvgvqQ5UQSzh5BE/Wp0u9bGzpUHgODeRtfO45miC+5itGBs0r1KKjN0CEzQ8WsDzWK2eGmmnznW8f2osEa83C9sa41/dEC5U1Cy8vpMgp/nz+qjKf+wQ3OUsSEgOKrjvC3tZcWUivhyu/GPEhAHEF6XBOTOyMnspoZKWNL4RMxGxfpeG3ANoer4vmdizPK7C6h3eLyTTYfL0jcML9Ld+rFKMD7hVx8ATu32nQVt3GmXa9m8cp2rSNPdgRV36LMWRxW2aXMq+MksRZkNhhm4WxSGUykR3N8K8jFiD9LPrz0pEux8UXEfF8ZWEKlxn2jCn9Tjy1WLMQ5ljotGHt+eMsfkaWoC</eBayAuthToken>\r\n" + 
            "  </RequesterCredentials>\r\n" + 
            "  <ItemID>"+ input + "</ItemID>\r\n" + 
            "</GetItemRequest>";
    URL url = new URL("https://api.ebay.com/ws/api.dll");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    // Set timeout as per needs
    connection.setConnectTimeout(20000);
    connection.setReadTimeout(20000);

    // Set DoOutput to true if you want to use URLConnection for output.
    // Default is false
    connection.setDoOutput(true);
    connection.setRequestMethod("POST");

    // Set Headers
    connection.setRequestProperty("Content-Type", "application/xml");
    connection.setRequestProperty("X-EBAY-SOA-SECURITY-APPNAME", "EdwardMu-CharityP-PRD-538907625-4999b865");
    connection.setRequestProperty("X-EBAY-API-SITEID", "3");
    connection.setRequestProperty("X-EBAY-API-CALL-NAME", "GetItem");
    connection.setRequestProperty("X-EBAY-API-COMPATIBILITY-LEVEL", "1107");
    connection.setRequestProperty("X-EBAY-API-APP-NAME", utilityClass.securityAppName);
    connection.setRequestProperty("X-EBAY-API-DEV-NAME", utilityClass.devName);
    connection.setRequestProperty("X-EBAY-API-CERT-NAME", utilityClass.certName);

    // Write XML
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody.getBytes("UTF-8"));
    outputStream.flush();
    outputStream.close();

    // Read XML
    InputStream inputStream = connection.getInputStream();
    StringBuilder responseBuilder = new StringBuilder();
    byte[] res = new byte[2048];
    int i;
    while ((i = inputStream.read(res)) != -1) {
        responseBuilder.append(new String(res, 0, i));
    }
    inputStream.close();
    return responseBuilder.toString();
}

返回正常的xml响应,例如 <200,Success1.0.02019-12-05T13:02:20.236Z <>等

任何帮助都会很棒,因为我发现很难解析其他响应

0 个答案:

没有答案
相关问题