如何在 Xamarin.PCL 项目中调用 Web 服务



我一直在xamarin网站上寻找,这是调用Web服务的数十种方法。到目前为止,每次我尝试重复教程中的示例时,都会出错。那么,我怎样才能简单地调用一个 php 网络服务,它返回一个我可以使用的 json?

这是我所做的:

 private async Task<JsonValue> Connexion_Webservice(string url)
        {
            // Creates the HTTP Request
            HttpWebRequest requete = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
            requete.ContentType = "application/json";
            requete.Method = "GET";

            //Sends the request and wait for the response
            using (WebResponse response = await requete.BeginGetResponse()
            {
             // until here I dont know what to do, what should I do ?   ;
            }
        }

这是示例

  using (WebResponse response = await request.GetResponseAsync ())
{
    // Get a stream representation of the HTTP web response:
    using (Stream stream = response.GetResponseStream ())
    {
        // Use this stream to build a JSON document object:
        JsonValue jsonDoc = await Task.Run (() => JsonObject.Load (stream));
        Console.Out.WriteLine("Response: {0}", jsonDoc.ToString ());
        // Return the JSON document:
        return jsonDoc;
    }
}

WebClient或httpClient也可以工作!

using (var webClient = new System.Net.WebClient()) {
var json = webClient.DownloadString(URL);  }

最新更新