Windows phone 7 WebClient未找到错误,但正在使用HttpWebRequest/Response

Windows phone 7 WebClient未找到错误,但正在使用HttpWebRequest/Response,windows-phone-7,Windows Phone 7,在我的WinPhone应用程序中,我正在访问REST服务。 一开始我使用的代码是: WebClient wc = new WebClient(); wc.Credentials = credentials; wc.Headers["App-Key"] = appKey; wc.DownloadStringCompleted += (o, args) => MessageBox.Show(args.Error == null ? "OK" : "Error"); wc.Downlo

在我的WinPhone应用程序中,我正在访问REST服务。 一开始我使用的代码是:

WebClient wc = new WebClient();
wc.Credentials = credentials;
wc.Headers["App-Key"] = appKey;
wc.DownloadStringCompleted += 
    (o, args) => MessageBox.Show(args.Error == null ? "OK" : "Error");
wc.DownloadStringAsync(uri);
但它突然停止了工作,并向我返回了一个“远程服务器返回了一个错误:NotFound”错误。在谷歌会话和控制面板中的一些点击之后,我没有让它工作。 我决定尝试另一种方式:

HttpWebRequest request = HttpWebRequest.CreateHttp(uri);
request.Credentials = credentials;
request.Headers["App-Key"] = appKey;
request.BeginGetResponse(asResult =>
    {
        var response = request.EndGetResponse(asResult) as HttpWebResponse;
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string responseString = reader.ReadToEnd();

        Dispatcher.BeginInvoke(
            () => MessageBox.Show(response.StatusCode.ToString()));
    }, null);
它是有效的

我还尝试运行了第一个截取,将URI指向google的主页,结果成功了(当然,我必须删除凭据)

有人能解释发生了什么事吗

更新

我设法通过更换电源使它工作

wc.Credentials = new NetworkCredentials(username, password);

但我仍然想知道发生了什么,第一行和第二行之间有什么区别


PS:测试URI为:但您需要他们提供一个应用程序密钥。

使用Credentials属性时,HttpWebRequest实现将在发送“Authorization”头值之前等待服务器的质询响应

但在某些情况下,这可能是一个问题,因此您必须通过直接提供授权标头来强制进行基本身份验证

使用REST客户端库时的示例,如:

RestTemplate=新的RestTemplate(“http://example.com");
添加(新的BasicSigningRequestInterceptor(“登录”,“密码”));
字符串结果=template.GetForObject(uri);
wc.Headers["Authorization"] = "Basic someBase64encodedString";
RestTemplate template = new RestTemplate("http://example.com");
template.RequestInterceptors.Add(new BasicSigningRequestInterceptor("login", "password"));
string result = template.GetForObject<string>(uri);