2017-10-05 219 views
0

我寫了下面的代碼發送POST請求到一個URL。當我運行代碼時,我得到500錯誤代碼。但是,當我使用下面的頭文件在SOAP UI中嘗試了相同的URL時,我得到了回覆。我可以知道我的代碼中有什麼問題嗎?提前致謝。我懷疑我沒有正確添加標題。HTTP POST請求失敗

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:arm="http://siebel.com/Webservice"> 
     <soapenv:Header> 
       <UsernameToken xmlns="http://siebel.com/webservices">username</UsernameToken> 
       <PasswordText xmlns="http://siebel.com/webservices">password</PasswordText> 
       <SessionType xmlns="http://siebel.com/webservices">Stateless</SessionType> 
      </soapenv:Header> 
      <soapenv:Body> 
      <arm:QueryList_Input> 
       <arm:SRNum></arm:SRNum> 
      </arm:QueryList_Input> 
      </soapenv:Body> 
     </soapenv:Envelope> 

以下是我的代碼。

 package com.siebel.Webservice; 

    import java.io.BufferedReader; 
    import java.io.DataOutputStream; 
    import java.io.InputStreamReader; 
    import java.net.HttpURLConnection; 
    import java.net.URL; 

    import javax.net.ssl.HttpsURLConnection; 

    public class HttpQueryList { 
     private final String USER_AGENT = "Mozilla/5.0"; 

     public static void main(String[] args) throws Exception { 

      HttpQueryList http = new HttpQueryList(); 



      System.out.println("\nTesting 2 - Send Http POST request"); 
      http.sendPost(); 

     } 



     // HTTP POST request 
     private void sendPost() throws Exception { 

      String url = "https://mywebsite.org/start.swe"; 
      URL obj = new URL(url); 
      HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 

      //add reuqest header 
      con.setRequestMethod("POST"); 
      con.setRequestProperty("UsernameToken", "username"); 
      con.setRequestProperty("PasswordText", "password"); 

      String urlParameters = "SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=1"; 

      // Send post request 
      con.setDoOutput(true); 
      DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
      wr.writeBytes(urlParameters); 
      wr.flush(); 
      wr.close(); 

      int responseCode = con.getResponseCode(); 
      System.out.println("\nSending 'POST' request to URL : " + url); 
      System.out.println("Post parameters : " + urlParameters); 
      System.out.println("Response Code : " + responseCode); 

      BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream())); 
      String inputLine; 
      StringBuffer response = new StringBuffer(); 

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

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

     } 

    } 
+0

看看這個:https://stackoverflow.com/questions/14522931/how-to-add-header-to-soap-request –

回答

1

在您的XML中,您指定了一個令牌。當我使用SOAP UI完成此操作時,我使用了一個證書文件。在我的情況下,我把它放在我的C:\ Program Files文件(x86)\ SmartBear \ SoapUI-5.2.1 \ bin文件夾中。然後我配置SOAP UI來使用它。你有證書嗎?如果是,你是否參考它?

+0

我沒有任何證書 – James