0

我正在开发 windows phone 8 应用程序。
我无法使用 system.net.httpwebrequest 使用 dll;
在 windows phone 8 中,所以我需要将 http 请求转换为 webclient 任何人都可以建议如何转换它..?

     private HttpWebResponse GetHttpWebResponse(HttpWebRequest webRequest)
                {
                    HttpWebResponse response;

                    try
                    {
                        response = (HttpWebResponse)webRequest.GetResponse();
    //GetResponse() produce error that System.Net.HttpRequest dll is missing, 
    //so im in need to conertr the http request to webclient.
                    }
                    catch (WebException we)
                    {
                        response = (HttpWebResponse)we.Response;
                    }

                    return response;
                }

my complete Json data

    [
      {
        "id": 01,
        "address": "12asdf",
        "city": " chennai",
        "contact1": "",
        "contact2": "",
        "country": " india",
        "description": "",
        "name": " david",
        "region": "",
        "state": "  033",
        "website": "",
        "image": "",
         "PrayerTime": {
          "id": 01,
          "PrayerTime1": "00:52",
          "PrayerTime2": "21:04",
          "PrayerTime3": "12:27",
          "PrayerTime4": "05:35",
          "PrayerTime5": "21:04",
          "created_at": null,
          "PrayerTime6": "04:01",
          "updated_at": null,
          "organization_id": 001
        }
      },.............
      }
4

1 回答 1

0

我建议您改用 HttpClient(nuget 包),它更方便,并且在 WinRT 上也受支持。

这是一个尝试从社交媒体获取地理编码数据的示例(这本身无关,但它是一个真实的示例:),使用 HttpClient

(您需要 Newtonsoft.Json 才能使用 JObject/JArray)

您可能可以放弃我在您自己的调用中添加 DefaultRequestHeaders 的部分

            using (HttpClient client = new HttpClient())
            {
                string url = mediaConfig.RequestUrl + String.Format(mediaConfig.GeoCodeStringFormat, lat, lon, distance);

                if (mediaConfig.MediaName == "Twitter")
                    client.DefaultRequestHeaders.Add(mediaConfig.BearerTokenParamName, mediaConfig.BearerToken);
                else if (mediaConfig.MediaName == "Instagram")
                    url = url + "&" + mediaConfig.BearerTokenParamName + "=" + mediaConfig.BearerToken;
                else if (mediaConfig.MediaName == "GooglePlaces")
                    url = url + "&" + mediaConfig.BearerTokenParamName + "=" + mediaConfig.BearerToken;
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

                response = await client.GetAsync(url);

                response.EnsureSuccessStatusCode();
            }
            responseString = await response.Content.ReadAsStringAsync();

如果要返回“responseString”,请确保您的方法签名具有 async 关键字:

    public async Task<string> methodname(... params ...

从同步方法中“使用”此方法:

var mytask = methodname();
    mytask.ContinueWith(c =>
    {
         var jsonResponse = JObject.Parse(c.Result);
         // or JArray
         var jsonResponseArray = JArray.Parse(c.Result);

         foreach (var item in jsonResponseArray)
         {
              var id = item.SelectToken("id").ToString();
              // and so on...

         }
         var selectSomething = jsonResponse.SelectToken("somethinghere");
         Deployment.Current.Dispatcher.BeginInvoke(() =>
         {
                 // do your ui tasks, navigate etc...
         });
    });
于 2013-11-08T10:57:28.210 回答