如何使用EditText字段(纯文本)的输入在服务器上编写/创建文本文件?

时间:2018-08-10 10:10:58

标签: c# android xamarin xamarin.android ftpwebrequest

所以我具有从服务器读取的功能:

add(1,32)

现在,我正在寻找写功能,但找不到任何东西。我试图将Web Request与SteamWriter连接起来,但是很混乱。

我认为我必须使用与读取功能完全不同的方法。有人可以分享他的C#,Xamarin天才吗?预先感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用System.Net库进行网络连接。另外,您没有执行此异步操作。您应该使按钮功能异步,如下所示。只需将编辑框的内容放入字符串内容字符串中即可。如果您还有其他问题,请告诉我。我也为您包括了使用语句。您可以在nuget上获取system.http。

https://www.nuget.org/packages/Microsoft.Net.Http/

using System;
using System.Text;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

async Task<string> ShowRemoteStringFile(string readUrl){

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", Username, Password))));
httpClient.DefaultRequestHeaders.Add("Accept", "text/plain; charset=UTF-8");
var body = new StringContent("your http post body text", Encoding.UTF8, "application/json");
Task<System.Net.Http.HttpResponseMessage> r = httpClient.PostAsync("YOUR_URL", body);
// do stuff while waiting for response to come back
var response = await r; //await the request to return a result from the server 
var responseContent = response.Content;
if (response.IsSuccessStatusCode){
//Do stuff with the response content
}

}

现在使按钮异步,以便您可以在按钮事件内等待:

btnRead.Click += async (sender, e) => 
{
    await ShowRemoteStringFile("string"); 
};

让我知道您是否有任何问题:)