技术开发 频道

应用程序向IIS传送身份验证



1.1.    Web services端代码

 

很简单的一个Web services,一个foo方法,输入一个整数,返回一个这个整数乘以十后的字符串,内容如下:

[WebService(Namespace = "http://chnking.com/")]

 

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

 

public class FOOSAMPLEService : System.Web.Services.WebService

 

{

 

    [WebMethod]

 

    public string foo(int a)

 

    {

 

        return (a*10).ToString();

 

}

 

}

 

1.2.    Soap调用web services的格式

 

发布为web services后,在浏览器浏览这个web servicesasmx文件,可以看到这个foo如何用soap协议进行调用。

 

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

 

POST /WS1/Service.asmx HTTP/1.1

 

Host: localhost

 

Content-Type: application/soap+xml; charset=utf-8

 

Content-Length: length

 


 

<?xml version="1.0" encoding="utf-8"?>

 

<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

 

  <soap12:Body>

 

    <foo xmlns="http://chnking.com/">

 

      <a>int</a>

 

    </foo>

 

  </soap12:Body>

 

</soap12:Envelope>

 

 

HTTP/1.1 200 OK

 

Content-Type: application/soap+xml; charset=utf-8

 

Content-Length: length

 


 

<?xml version="1.0" encoding="utf-8"?>

 

<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

 

  <soap12:Body>

 

    <fooResponse xmlns="http://chnking.com/">

 

      <fooResult>string</fooResult>

 

    </fooResponse>

 

  </soap12:Body>

 

</soap12:Envelope>

 

 

1.3.    客户端应用调用web services代码

 

下面代码在应用程序中用HttpWebRequest对象以上面web services要求的格式发送soap请求。服务端设置为只允许windows身份验证,HttpWebRequest对象同时传送用户凭据,最后接收服务端的返回的soap的结果。

 

//根据需要访问的URL新建HttpWebRequest对象

 

HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://localhost:81/ws1/Service.asmx");

 

//要发送soap请求的内容,必须使用post方法传送数据

 

myHttpWebRequest.Method = "POST";

 

//缺省当前登录用户的身份凭据

 

myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials;

 

 

//准备soap请求的内容

 

string cont;

 

cont =  "<?xml version='1.0' encoding='utf-8'?>";

 

cont += "<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'>";

 

cont += "  <soap12:Body>";

 

cont += "    <foo xmlns='http://chnking.com/'>";

 

cont += "      <a>1</a>";

 

cont += "    </foo>";

 

cont += "  </soap12:Body>";

 

cont += "</soap12:Envelope>";

 

 

byte[] byteRequest = Encoding.UTF8.GetBytes(cont);

 

 

myHttpWebRequest.ContentType = "application/soap+xml; charset=utf-8";

 

myHttpWebRequest.ContentLength = byteRequest.Length;

 

myHttpWebRequest.Timeout = 100000;

 

 

//soap请求的内容放入HttpWebRequest对象post方法的请求数据部分

 

Stream newStream = myHttpWebRequest.GetRequestStream();

 

newStream.Write(byteRequest, 0, byteRequest.Length);

 

 

//发送请求

 

HttpWebResponse myWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

 

 

//将收到的回应从Stream转换成string

 

newStream = myWebResponse.GetResponseStream();

 

byte[] byteResponse = new byte[myWebResponse.ContentLength];

 

newStream.Read(byteResponse, 0, (int)myWebResponse.ContentLength);

 

 

//str里面就是返回soap回应的字符串了

 

string str = Encoding.UTF8.GetString(byteResponse);

 

 

myWebResponse.Close();

 

返回的str的结果是:

 

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><fooResponse xmlns="http://chnking.com/"><fooResult>10</fooResult></fooResponse></soap:Body></soap:Envelope>

 

 

0
相关文章