2013-05-02 122 views
1

我想通过SSL向SOAP服务器(Microsoft IIS服务器)发送SOAP请求。当我通过使用SSL - Keystore配置的soapUI工具测试SOAP请求时,它会正确返回响应。但使用下面的代码,它返回“HTTP/1.1 400错误请求” 我使用httpclient-4.2.3和httpcore-4.2.2。使用SSL在ApacheHttpClient中发送SOAP请求时的问题

import java.security.KeyStore;  
import org.apache.http.Header;  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.conn.scheme.Scheme;  
import org.apache.http.conn.scheme.SchemeRegistry;  
import org.apache.http.conn.ssl.SSLSocketFactory;  
import org.apache.http.entity.StringEntity;  
import org.apache.http.impl.client.DefaultHttpClient;  
import org.apache.http.impl.conn.BasicClientConnectionManager;  
import org.apache.http.params.BasicHttpParams;  
import org.apache.http.params.HttpParams;  
import org.apache.http.util.EntityUtils; 


public final static void main(String[] args) throws Exception { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     try { 

     KeyStore keyStore = KeyStore.getInstance("JKS"); 
     FileInputStream instream1 = new FileInputStream(new File("C:\\CCDKeyStore\\mykeystore.jks")); 
     try { 
      keyStore.load(instream1, "1214524".toCharArray()); 

     } finally { 
      try { instream1.close(); } catch (Exception ignore) {} 
     } 

     SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore,"1214524"); 
     Scheme sch = new Scheme("https", 443, socketFactory); 
     SchemeRegistry schemeRegistry = httpclient.getConnectionManager().getSchemeRegistry(); 
     schemeRegistry.register(sch); 

     final HttpParams httpParams = new BasicHttpParams(); 
     DefaultHttpClient lHttpClient = new DefaultHttpClient(new BasicClientConnectionManager(schemeRegistry), httpParams); 

     String lUrl = "https://api.demo.server.com/XDS/Service.svc?wsdl"; 

     StringBuffer lXmlBuffer = new StringBuffer(); 

     lXmlBuffer.append("<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\">\n"); 
     lXmlBuffer.append("<s:Header>\n"); 
     lXmlBuffer.append("<a:Action s:mustUnderstand=\"1\">urn:74347:4757:StoredQuery</a:Action>\n"); 
     lXmlBuffer.append("<a:MessageID>urn:uuid:c6430690-412e-4744-afe1-233e2138f2d2</a:MessageID>\n"); 
     . 
     . 
     . 
     . 
     . 
     . 
     lXmlBuffer.append("</Slot>\n"); 
     lXmlBuffer.append("</AdhocQuery>\n");    
     lXmlBuffer.append("</query:AdhocQueryRequest>\n"); 
     lXmlBuffer.append("</s:Body>\n"); 
     lXmlBuffer.append("</s:Envelope>\n");   


     String lXml = lXmlBuffer.toString(); 


     HttpPost lMethod = new HttpPost(lUrl); 
     HttpEntity lEntity = new StringEntity(lXml, "multipart/related", "utf-8"); 

     lMethod.setHeader("SOAPAction", "urn:74347:4757:StoredQuery"); 
     System.out.println(EntityUtils.toString(lEntity)); 
     lMethod.setEntity(lEntity); 

     HttpResponse lHttpResponse = lHttpClient.execute(lMethod); 




     } finally { 

      httpclient.getConnectionManager().shutdown(); 
     } 

任何帮助高度赞赏

感谢, 磨憨

+0

您确定SOAP Envelope格式正确吗? – acdcjunior 2013-05-02 14:58:13

+0

是的,当我复制并通过上面的类生成相同的SOAP请求到soapUI它正确返回响应... – Mohan 2013-05-03 05:08:17

回答

0

最后我找到了答案......

在这里,在上面的代码下面一行包含内容类型为“多/有关”。但是,由于这是SOAP请求,因此期望Web服务的内容类型为“application/soap + xml”。请参阅w3schools tutorial以获取更多信息

HttpEntity lEntity = new StringEntity(lXml,“multipart/related”,“utf-8”);