1

我想通过 SSL 向 SOAP 服务器(Microsoft IIS 服务器)发送一个 SOAP 请求。当我通过带有 SSL - Keystore 配置的 soapUI 工具测试 SOAP 请求时,它会正确返回响应。但是使用以下代码返回“HTTP/1.1 400 Bad Request”,我使用了 httpclient-4.2.3 和 httpcore-4.2.2。

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();
        }

对此高度赞赏的任何帮助

谢谢,莫汉

4

1 回答 1

0

终于我找到了答案……

上面代码中的以下行包含内容类型为“multipart/related”。但是 Web 服务应该是内容类型“application/soap+xml”,因为这是 SOAP 请求。请参阅w3schools 教程以获取更多信息

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

于 2013-05-29T10:05:26.877 回答