2017-03-02 253 views
0

我有我的基於Rest的服務,我必須將XML傳遞給它,當我在RestClient ans郵遞員中運行時,它運行正常,但是當我在代碼中運行它時,它給了我400錯誤請求代碼在下面給出的任何形式的幫助將是有益的。謝謝HTTP/1.1 400錯誤請求400

public static void callservice() throws URISyntaxException { 
    String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"; 
    HttpClient httpClient = new DefaultHttpClient(); 
    try { 

     HttpPost post = new HttpPost(); 
     URI as = new URI("http://172.24.1.137:8280/finalApi/v1.0.0"); 

     post.setURI(as); 
     post.setHeader("Authorization", 
       "Bearer 62fbb099-af9c-35a4-b8e5-82adf92ae9bd"); 
     post.setHeader("SOAPAction", 
       "http://tempuri.org/IService/accountbalance"); 
     post.setHeader("content-type", "text/xml; charset=utf-8"); 
     post.setHeader("cache-control", "no-cache"); 

     List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); 
     StringBuffer xmlString = new StringBuffer(); 

     xmlString 
       .append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"); 
     xmlString.append("<soapenv:Header/>"); 
     xmlString.append("<soapenv:Body>"); 
     xmlString.append("<tem:accountbalance>"); 
     xmlString.append("<tem:accountNo>051000207960141</tem:accountNo>"); 
     xmlString.append("</tem:accountbalance>"); 
     xmlString.append("</soapenv:Body>"); 
     xmlString.append("</soapenv:Envelope>"); 
     String stw = new String(
       "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\r\n <soapenv:Header/>\r\n <soapenv:Body>\r\n  <tem:accountbalance>\r\n   <!--Optional:-->\r\n   <tem:accountno>051000207960141</tem:accountno>\r\n  </tem:accountbalance>\r\n </soapenv:Body>\r\n</soapenv:Envelope>"); 
     urlParameters.add(new BasicNameValuePair("xmldoc", xmlString 
       .toString())); 

     System.out.println(xmlString); 
     // urlParameters.add(new BasicNameValuePair("xml", 
     // xmlString.toString())); 
     post.setEntity(new UrlEncodedFormEntity(urlParameters)); 
     // Execute HTTP request 

     HttpResponse httpResponse = httpClient.execute(post); 

     System.out.println("----------------------------------------"); 
     System.out.println(httpResponse.getStatusLine()); 
     System.out.println(httpResponse.getStatusLine().getStatusCode()); 
     System.out.println(httpResponse.getParams()); 
     System.out.println("----------------------------------------"); 

     // Get hold of the response entity 
     HttpEntity entity = httpResponse.getEntity(); 

     // If the response does not enclose an entity, there is no need 
     // to bother about connection release 
     byte[] buffer = new byte[1024]; 
     if (entity != null) { 
      InputStream inputStream = entity.getContent(); 
      try { 
       int bytesRead = 0; 
       BufferedInputStream bis = new BufferedInputStream(
         inputStream); 
       while ((bytesRead = bis.read(buffer)) != -1) { 
        String chunk = new String(buffer, 0, bytesRead); 
        System.out.println(chunk); 
       } 
      } catch (IOException ioException) { 
       // In case of an IOException the connection will be released 
       // back to the connection manager automatically 
       ioException.printStackTrace(); 
      } catch (RuntimeException runtimeException) { 
       // In case of an unexpected exception you may want to abort 
       // the HTTP request in order to shut down the underlying 
       // connection immediately. 
       post.abort(); 
       runtimeException.printStackTrace(); 
      } finally { 
       // Closing the input stream will trigger connection release 
       try { 
        inputStream.close(); 
       } catch (Exception ignore) { 
       } 
      } 
     } 
    } catch (ClientProtocolException e) { 
     // thrown by httpClient.execute(httpGetRequest) 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // thrown by entity.getContent(); 
     e.printStackTrace(); 
    } finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpClient.getConnectionManager().shutdown(); 
    } 
} 

回答