简体   繁体   English

如何使用Microsoft.Net.Http上传数据?

[英]How to upload data with Microsoft.Net.Http?

In the past I made a class that shunk the request on an endpoint . 过去,我制作了一个在endpoint上缩减请求的类。 Now, I create a dll that include this method, this is the code that I'm trying to convert on this library: 现在,我创建一个包含此方法的dll ,这是我要在此库上转换的代码:

using (var client = new HttpClient())
{
     string requestJson = JsonConvert.SerializeObject(data);
     client.DefaultRequestHeaders.Add("token", token);
     byte[] responseArray = client. 'there is no upload data method
     // the bottom code is of the old method 
     byte[] responseArray = client.UploadData(requestURI, method, Encoding.UTF8.GetBytes(requestJson));
    return Encoding.ASCII.GetString(responseArray);
}

In the not portable library System.Net I can call client.UploadData , but here I see only : postAsync and putAsync, there is a method that independent from the put or post request allow me to send the data from the client to the server ? 在非便携式库System.Net我可以调用client.UploadData ,但是在这里我只能看到:postAsync和putAsync,有一种方法独立于putpost请求,使我可以将数据从client发送到server Thanks in advance. 提前致谢。

In an HTTP request PUT and POST are the correct ways to transmit data to a server, it does not make sense to send data independently of these methods. 在HTTP请求中,PUT和POST是将数据传输到服务器的正确方法,而独立于这些方法发送数据是没有意义的。 When you are using a client such as that available in System.Net this is merely being abstracted away from you. 当您使用诸如System.Net中可用的客户端时,这仅仅是从您身上抽象出来的。

In your old code you used some method passed in method parameter to send data with UploadData method, and it was probably POST or PUT . 在您的旧代码中,您使用了一些传入method参数的方法来通过UploadData方法发送数据,它可能是POSTPUT If you do not specify the method for UploadData , POST is being used. 如果未为UploadData指定方法,则使用POST So you should use PostAsync or PutAsync , based on you current code and the value of method parameter you pass to UploadData . 所以,你应该用PostAsyncPutAsync ,根据您当前的代码和值method参数传递给UploadData

The simplest way would be to use something like this: 最简单的方法是使用如下所示的内容:

using(var client = new HttpClient())
{
    var response = await client.PostAsJsonAsync(requestUrl, data);
    return await response.Content.ReadAsStringAsync();
}

The code for PUT would be the same, but with PutAsJsonAsync PUT的代码将相同,但使用PutAsJsonAsync

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 5 .NET HTTP库-Microsoft.Net.Http放在哪里? - 5 .NET HTTP libraries - where does Microsoft.Net.Http fit in? System.Net.Http 与 Microsoft.Net.Http - System.Net.Http vs Microsoft.Net.Http 如何将此.NET RestSharp代码转换为Microsoft.Net.Http HttpClient代码? - How can I convert this .NET RestSharp code to Microsoft.Net.Http HttpClient code? 如何通过Nuget将Microsoft.Net.Http安装到Xamarin.Forms PCL项目? - How do I install Microsoft.Net.Http via Nuget to Xamarin.Forms PCL project? 使用Microsoft.Net.Http将文件发送到服务 - Send file to service using Microsoft.Net.Http Xamarin PCL和HttpClient的Microsoft.Net.Http在Android AND iOS上受支持吗? - Xamarin PCL and Microsoft.Net.Http for HttpClient is this supported on Android AND iOS? VS 2013中找不到'Microsoft.Net.Http'程序包 - Unable to find 'Microsoft.Net.Http' package in VS 2013 Silverlight 5中postAsync上的可移植Microsoft.Net.Http对象引用错误 - Portable Microsoft.Net.Http object reference error on postAsync in Silverlight 5 无法通过Nuget安装Microsoft.Net.Http - Unable to Install Microsoft.Net.Http through Nuget 为什么 Xamarin 应用程序看不到使用 microsoft.net.http - Why Xamarin app not see using microsoft.net.http
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM