当前位置: 代码迷 >> Web前端 >> java代码透过HttpClient发送WebService请求
  详细解决方案

java代码透过HttpClient发送WebService请求

热度:197   发布时间:2012-08-22 09:50:35.0
java代码通过HttpClient发送WebService请求
import java.io.ByteArrayInputStream;   
import java.io.InputStream;   
  
import org.apache.commons.httpclient.HttpClient;   
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;   
import org.apache.commons.httpclient.methods.PostMethod;   
import org.apache.commons.httpclient.methods.RequestEntity;   
  
public class Saml_Test {   
    private static final String soapRequestData =   
"       <SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"  
+"      <SOAP:Header>"  
+"      <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"  
+"          <wsse:UsernameToken xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"  
+"              <wsse:Username>administrator</wsse:Username>"  
+"              <wsse:Password>cordys</wsse:Password>"  
+"          </wsse:UsernameToken>"  
+"      </wsse:Security>"  
+"  </SOAP:Header>"  
+"  <SOAP:Body>"  
+"      <samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" MinorVersion=\"1\">"  
+"          <samlp:AuthenticationQuery>"  
+"              <saml:Subject xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\">"  
+"                  <saml:NameIdentifier Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\">administrator</saml:NameIdentifier>"  
+"              </saml:Subject>"  
+"          </samlp:AuthenticationQuery>"  
+"      </samlp:Request>"  
+"  </SOAP:Body>"  
+"</SOAP:Envelope>";   
    public static void main(String[] args) {   
        try {   
            System.out.println(soapRequestData);   
            PostMethod postmethod = new PostMethod(   
                    "http://10.200.1.128/cordys/com.eibus.web.soap.Gateway.wcp");   
            byte[] b = soapRequestData.getBytes("UTF-8");   
            InputStream is = new ByteArrayInputStream(b, 0, b.length);   
            RequestEntity re = new InputStreamRequestEntity(is, b.length,   
                    "application/xop+xml; charset=UTF-8; type=\"text/xml\"");   
            postmethod.setRequestEntity(re);   
            HttpClient httpClient = new HttpClient();   
            int statusCode = httpClient.executeMethod(postmethod);   
            System.out.println("statuscode=" + statusCode);   
            String soapResponseData = postmethod.getResponseBodyAsString();   
            System.out.println(soapResponseData);   
        } catch (Exception ex) {   
            ex.printStackTrace();   
        }   
    }   
} 
  相关解决方案