【问题标题】:Retrieve JSON string from URL in Windows Phone 8从 Windows Phone 8 中的 URL 检索 JSON 字符串
【发布时间】:2015-06-18 15:08:30
【问题描述】:

我正在尝试从 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:异常 在 WebClient 请求期间发生。 ---> System.Net.ProtocolViolationException:使用此方法的请求 不能有请求正文。在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback 开始方法,对象状态)在 System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) 在 System.Net.WebClient.GetWebResponse(WebRequest 请求,IAsyncResult 结果)在 System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult 结果) --- 内部异常堆栈跟踪结束 ---} System.Net.WebException

我尝试过使用 HttpClient,但遇到了同样的问题。我想知道是否有人知道如何解决这个问题。

谢谢!

更新:我尝试使用 IE 导航到手机上的页面,然后 IE Mobile 说:“不支持的地址,IE Mobile 不支持这种类型的地址,可以” t 显示此页面”。这就是应用程序崩溃的原因?

【问题讨论】:

  • 当您尝试使用另一个地址时发生了什么?
  • 我得到了同样的错误(页面 URL 类似于:“180.200.1.231:4567/...")。现在我尝试添加 client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip",然后我得到一个“未找到服务器异常”
  • 尝试添加 http://180. ....
  • 我正在这样做,我正在解析的 URL 有“http://”,但我什至无法获取 Json 字符串。我不知道我是否犯了一些代码错误,或者代码中是否缺少某些东西。

标签: c# .net json windows-phone-8 webclient


【解决方案1】:

GET 方法(带/不带凭据)

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 方法(带/不带凭据)

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);
        }
}

【讨论】:

    猜你喜欢
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多