我想从一个网址JSON字符串中的Windows Phone 8应用程序(页面需要验证,我想这是我的代码失败),我使用下面的代码:
public WebClient client = new WebClient();
public string result;
public void DoStuff()
{
string username = "username";
string password = "password";
string url = "myurl";
client.Credentials = new NetworkCredential(username, password);
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(url));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
result = e.Result;
}
但是,当运行应用程序时,我得到 System.Reflection.TargetInvocationException at e.result
进入 InnerException 我看到了这个:
[System.Net.WebException] {System.Net.WebException:WebClient请求期间发生异常 . ---> System.Net.ProtocolViolationException:使用此方法的请求不能有请求正文 . 在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态)在System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult的asyncResult)在System.Net.WebClient.GetWebResponse(WebRequest的请求,IAsyncResult的结果)在System.Net .WebClient.DownloadBitsResponseCallback(IAsyncResult result)---内部异常堆栈跟踪结束---} System.Net.WebException
我尝试过使用HttpClient,但我遇到了同样的问题 . 我想知道是否有人知道如何解决这个问题 .
谢谢!
UPDATE: 我've tried navigating to the page on my phone using IE, and then the IE Mobile says: 2602987 . That'为什么应用程序也崩溃了?
1 回答
GET Method (with/without Credentials)
POST Method (with/without Credentials)