c# - Android 等同于 .NET 中的 WebClient

标签 c# .net android webclient

我有一个相当基础的应用程序,是我之前用 C# NET 编写的,我想为 Android 平台重写它。它只是使用一些 Web 软件公开的 API,我可以通过 .NET 中的 WebClient 访问它。

WebClient myClient = new WebClient();

//Prepare a Name/Value Collection to hold the post values
NameValueCollection form = new NameValueCollection();
form.Add("username", "bob");
form.Add("password", GetMD5Hash("mypass"));
form.Add("action", "getusers");

// POST data and read response
Byte[] responseData = myClient.UploadValues("https://mysite.com/api.php", form);
string strResponse = Encoding.ASCII.GetString(responseData);

我找到了 WebKit (android.webkit | Android Developers),但快速浏览似乎并不合适。

有没有人有任何关于如何将其移植过来的示例代码?

最佳答案

这可能是等价的:

List<NameValuePair> form = new ArrayList<NameValuePair>();
form.add(new BasicNameValuePair("username", "bob");
// etc...

// Post data to the server
HttpPost httppost = new HttpPost("https://mysite.com/api.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httppost);

总结HttpClient可以替换您的 WebClient。然后你必须使用 HttpPost用于发布数据或 HttpGet用于检索数据。您可以阅读一些教程来帮助您了解如何像这样使用这些类 one .

关于c# - Android 等同于 .NET 中的 WebClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6398465/

相关文章:

c# - 以任何形状裁剪图像

c# - 如何使用 Moq 库创建 SerialPort 模拟?

c# - 这段代码怎么可能: "ArgumentOutOfRangeException: startIndex cannot be larger than length of string"?

java - :mobile:compileDebugJavaWithJavac FAILED

android - 自定义图像以在 ActionBarSherlock 中占据最大宽度

Android 与整个应用共享 firestore 数据

c# - 通过添加新接口(interface)修复循环依赖

c# - 如何从已签名的 .Net 程序集中读取数字签名信息?

c# - 使用 Xamarin.Mac 将窗口置于前面

c# - 如何在 Lucene.NET 中搜索 Field.Index.NOT_ANALYZED 字段?