从 Windows Phone 中的 URL 检索 JSON 字符串 8
Retrieve JSON string from URL in Windows Phone 8
我正在尝试从 windows phone 8 应用程序中的 Url 获取 Json 字符串(该页面需要身份验证,我认为这就是我的代码失败了),我正在使用以下代码:
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;
}
然而,当 运行 应用程序时,我在 e.result 得到了一个 System.Reflection.TargetInvocationException
进入 InnerException 我看到了这个:
[System.Net.WebException] {System.Net.WebException: An exception
occurred during a WebClient request. --->
System.Net.ProtocolViolationException: A request with this method
cannot have a request body. at
System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback
beginMethod, Object state) at
System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult
asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest
request, IAsyncResult result) at
System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)
--- End of inner exception stack trace ---} System.Net.WebException
我试过使用 HttpClient,但我遇到了同样的问题。我想知道是否有人知道如何解决这个问题。
谢谢!
更新: 我试过使用 IE 导航到 phone 上的页面,然后 IE Mobile 显示:"Unsupported Address, IE Mobile doesn't support this type of address and can't display this page"。这就是应用程序也崩溃的原因?
GET 方法(with/without 凭据)
private string username = "user";
private string password = "passkey";
private string myUrl = "http://some_url.com/id?=20";
private WebClient client = new WebClient();
private void retrieveJson()
{
client.Credentials = new NetworkCredential(username, password);
client.Encoding = Encoding.UTF8;
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(myUrl), UriKind.Relative);
}
//WebClient String (json content) Download
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
// You will get you Json Data here..
var myJSONData = e.Result;
JObject JsonData = JObject.Parse(myJSONData);
//Do something with json
//Ex: JsonData["Array1"][5]
}
POST 方法(with/without 凭据)
private string username = "user";
private string password = "passkey";
private string myUrl = "http://some_url.com/id?=20";
private WebClient client = new WebClient();
private string parameters = "{\"I_NAME\":\"0000"\"}"; //Example
private void postMethod()
{
client.Credentials = new NetworkCredential(username, password);
client.Encoding = Encoding.UTF8;
client.Headers["Content-Length"] = parameters.Length.ToString();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
client.UploadStringAsync(new Uri(myUrl), "POST", parameters);
}
private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error == null)
{
var myJSONData = e.Result;
JObject JsonData = JObject.Parse(myJSONData);
}
}
我正在尝试从 windows phone 8 应用程序中的 Url 获取 Json 字符串(该页面需要身份验证,我认为这就是我的代码失败了),我正在使用以下代码:
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;
}
然而,当 运行 应用程序时,我在 e.result 得到了一个 System.Reflection.TargetInvocationException
进入 InnerException 我看到了这个:
[System.Net.WebException] {System.Net.WebException: An exception occurred during a WebClient request. ---> System.Net.ProtocolViolationException: A request with this method cannot have a request body. at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result) --- End of inner exception stack trace ---} System.Net.WebException
我试过使用 HttpClient,但我遇到了同样的问题。我想知道是否有人知道如何解决这个问题。
谢谢!
更新: 我试过使用 IE 导航到 phone 上的页面,然后 IE Mobile 显示:"Unsupported Address, IE Mobile doesn't support this type of address and can't display this page"。这就是应用程序也崩溃的原因?
GET 方法(with/without 凭据)
private string username = "user";
private string password = "passkey";
private string myUrl = "http://some_url.com/id?=20";
private WebClient client = new WebClient();
private void retrieveJson()
{
client.Credentials = new NetworkCredential(username, password);
client.Encoding = Encoding.UTF8;
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
client.DownloadStringAsync(new Uri(myUrl), UriKind.Relative);
}
//WebClient String (json content) Download
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
// You will get you Json Data here..
var myJSONData = e.Result;
JObject JsonData = JObject.Parse(myJSONData);
//Do something with json
//Ex: JsonData["Array1"][5]
}
POST 方法(with/without 凭据)
private string username = "user";
private string password = "passkey";
private string myUrl = "http://some_url.com/id?=20";
private WebClient client = new WebClient();
private string parameters = "{\"I_NAME\":\"0000"\"}"; //Example
private void postMethod()
{
client.Credentials = new NetworkCredential(username, password);
client.Encoding = Encoding.UTF8;
client.Headers["Content-Length"] = parameters.Length.ToString();
client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
client.UploadStringAsync(new Uri(myUrl), "POST", parameters);
}
private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error == null)
{
var myJSONData = e.Result;
JObject JsonData = JObject.Parse(myJSONData);
}
}