gpt4 book ai didi

Xamarin Forms PCL - 干净、简单的网络请求方法?

转载 作者:行者123 更新时间:2023-12-02 10:41:38 28 4
gpt4 key购买 nike

我正在使用可移植类库构建 Android/iOS xamarin 表单应用程序。我正在寻找在 PCL 项目中执行此示例的最佳方法:

https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Create a request for the URL.
WebRequest request = WebRequest.Create (
"http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
//do something with the response string

// Clean up the streams and the response.
reader.Close ();
response.Close ();
}
}
}

最佳答案

Flurl.Http (免责声明:我是作者)是一个与 Xamarin 兼容的 PCL,它使此类事情变得非常简单:

string s = await "http://www.contoso.com/default.html".GetStringAsync();

获取NuGet .

关于Xamarin Forms PCL - 干净、简单的网络请求方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31825627/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com