1

我正在编写一个简单的 Silverlight 应用程序,其中包含以下代码,我认为这是非常标准的:

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
var request = new WebClient();

var cred = new NetworkCredential(Server.UserName, Server.Password);
request.Credentials = cred;
request.UseDefaultCredentials = false;

request.DownloadStringCompleted += TestServerCompleted;
var uri = new Uri(Server.GetRequestUrl(Methods.ping));
request.DownloadStringAsync(uri);

然而,当我在 Fiddler 中查看请求时,标题中没有添加任何凭据。我错过了什么?那里不应该有一个“授权:基本......”标题吗?

4

1 回答 1

0

试试这样的东西。

        HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.UseDefaultCredentials = false;
        req.Credentials = ew NetworkCredential(Server.UserName, Server.Passwor

        req.ContentType = "text/xml;charset=\"utf-8\"";
        req.Accept = "text/xml";
        req.Method = "POST";
        return req;

        req.BeginGetResponse((IAsyncResult asynchronousResultResponse) =>
            {
                try
                {
                    HttpWebRequest requestResponse = (HttpWebRequest)asynchronousResultResponse.AsyncState;
                    HttpWebResponse response = (HttpWebResponse)requestResponse.EndGetResponse(asynchronousResultResponse);
                    Stream streamResponse = response.GetResponseStream();
                    StreamReader streamRead = new StreamReader(streamResponse);
                    string responseString = streamRead.ReadToEnd();

                    //Your response is here in responseString

                    streamResponse.Close();
                    streamRead.Close();
                    response.Close();
                }
                catch (Exception e)
                {
                    Callback(null, e);
                }
            }, webRequest);

我希望它可以帮助,即使是 2 个月后...

于 2011-04-22T15:15:06.007 回答