C# 如何在UWP中将字符串发布到URL

C# 如何在UWP中将字符串发布到URL,c#,wpf,visual-studio,uwp,httprequest,C#,Wpf,Visual Studio,Uwp,Httprequest,我想发布一个字符串到一个URL,这样我可以上传一个文件。 我在WPF项目中做过,我想在UWP项目中做。 这是我在WPF中的方法: OpenFileDialog ofd = new OpenFileDialog(); string url = "http://localhost/visualStudioUpload/upload1.php "; WebClient Client = new WebClient(); WebRequest request =

我想发布一个字符串到一个URL,这样我可以上传一个文件。 我在WPF项目中做过,我想在UWP项目中做。 这是我在WPF中的方法:

  OpenFileDialog ofd = new OpenFileDialog();

  string url = "http://localhost/visualStudioUpload/upload1.php ";

  WebClient Client = new WebClient();
            WebRequest request = WebRequest.Create(url);
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            byte[] byteArray = Client.UploadFile(url, "POST", ofd.FileName);

           request.ContentLength = byteArray.Length;

            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();

            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            //                  dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.

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

您可以使用
HttpClient
上传文件(它取代了UWP中的
WebClient

代码:

专用异步任务上载映像(字节[]文件,Uri url)
{
使用(var client=new HttpClient())
{
MultipartFormDataContent form=新的MultipartFormDataContent();
var content=新的流内容(新的内存流(文件));
添加(内容,“postname”,“filename.jpg”);
var response=wait client.PostAsync(url、表单);
return wait response.Content.ReadAsStringAsync();
}
}

您是否尝试过使用
HttpClient
类?这个问题很难理解。你到底在问什么-1我想向url发送一个字符串,以便将文件上载到服务器。我在WPF项目中维护了该文件,我想在UWP项目中执行该操作,但失败了。@FlorianMoser我尝试了许多解决方案,但均无效。我可能会向我们展示您迄今为止在UWP中的尝试,并展示您如何在端点中接收该文件。
private async Task<string> UploadImage(byte[] file, Uri url)
{
    using (var client = new HttpClient())
    {
        MultipartFormDataContent form = new MultipartFormDataContent();
        var content = new StreamContent(new MemoryStream(file));
        form.Add(content, "postname", "filename.jpg");
        var response = await client.PostAsync(url, form);
        return await response.Content.ReadAsStringAsync();
    }
}