使用SSL在ApacheHttpClient中发送SOAP请求时出现问题

时间:2013-05-02 14:54:57

标签: soap ssl apache-httpclient-4.x

我想通过SSL向SOAP服务器(Microsoft IIS服务器)发送SOAP请求。当我通过soapUI工具使用SSL-Keystore配置测试SOAP请求时,它会正确返回响应。但使用以下代码返回“HTTP / 1.1 400错误请求” 我使用了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();
        }

对此的任何帮助都非常感激

谢谢, 莫汉

1 个答案:

答案 0 :(得分:0)

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

此处上面代码中的以下行包含内容类型为“multipart / related”。但Web服务的内容类型为“application / soap + xml”,因为这是SOAP请求。有关详细信息,请参阅w3schools tutorial

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