我编写了一个处理程序类,我想知道如何验证身份验证。我还写了一篇测试 XML 帖子,其中包含了一些网络凭据。使用 Handler 读取/验证这些网络凭据需要什么?
处理程序:
public class HelloWorldHandler : IHttpHandler {
public HelloWorldHandler() {}
public void ProcessRequest(HttpContext context){
HttpRequest request = context.Request;
HttpResponse response = context.Response;
var stream = context.Request.InputStream;
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
string xml = Encoding.UTF8.GetString(buffer);
}
public bool IsReusable
{
// To enable pooling, return true here.
// This keeps the handler in memory.
get { return false; }
}
XML 帖子片段:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(xml);
req.Method = "POST";
req.ContentType = "text/xml;charset=utf-8";
req.ContentLength = requestBytes.Length;
req.Credentials = new NetworkCredential("Test", "Password");
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);