读书人

怎么发送 HTTP请求调用使用了soaphead

发布时间: 2012-12-16 12:02:32 作者: rapoo

如何发送 HTTP请求调用使用了soapheader验证的 Web Service
如何发送 HTTP请求调用使用了soapheader验证的 Web Service

Web Service中使用了简单的soapheader验证,现在要通过发送http请求调用,该如何操作呢?

[最优解释]
HttpWebRequest 调用soapheader验证的web service。你需要自己构造完整的soap message。 例如

例如:

HttpWebRequest request = (HttpWebRequest)
HttpWebRequest.Create(url);

String xmlString = txtInput.Text;
ASCIIEncoding encoding = new ASCIIEncoding();

byte[] bytesToWrite = encoding.GetBytes(xmlString);

request.Method = "POST";

request.ContentLength = bytesToWrite.Length;
request.Headers.Add("SOAPAction: \"http://localhost/XXX/ActionName\""); //You need to change this

request.ContentType = "text/xml; charset=utf-8";

Stream newStream = request.GetRequestStream();
newStream.Write(bytesToWrite, 0, bytesToWrite.Length);
newStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string responseFromServer = reader.ReadToEnd();

xmlString 的内容应该像是这样的:
var xmlString = @"<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<AuthSoapHd xmlns="www.XMLWebServiceSoapHeaderAuth.net">
<strUserName>string</strUserName>
<strPassword>string</strPassword>
</AuthSoapHd>
</soap:Header>
<soap:Body>
<HelloWorld xmlns="www.XMLWebServiceSoapHeaderAuth.net">
<name>string</name>
</HelloWorld>
</soap:Body>
</soap:Envelope>"

[其他解释]
补充下,Web Service使用的是简单的用户名密码验证,非windows验证。
[其他解释]
只能用post模式的吗

读书人网 >Web Service

热点推荐