首页 文章

从Windows Phone 8中的URL检索JSON字符串

提问于
浏览
1

我想从一个网址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 回答

  • 1

    GET Method (with/without Credentials)

    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 Method (with/without Credentials)

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

相关问题