使用 RestSharp 上传没有 Multipart/Form-Data 的文件 [英] Upload File Without Multipart/Form-Data Using RestSharp

查看:96
本文介绍了使用 RestSharp 上传没有 Multipart/Form-Data 的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是使用标准 HttpWebRequest 和 HttpWebResponse 完成的 POST 请求.基本上它发布带有一些参数的二进制文件.

Below, there is a POST Request Done With Standard HttpWebRequest , and HttpWebResponse. Basically it post binanry file with some parameters.

我怎样才能用 RestSharp 做同样的事情?

How Can I do The Same Thing With RestSharp ?

来源:

https://github.com/attdevsupport/ATT_APIPlatform_SampleApps/tree/master/RESTFul/SpeechCustom, ATT_APIPlatform_SampleApps/RESTFul/Speech/Csharp/app1/]

https://github.com/attdevsupport/ATT_APIPlatform_SampleApps/tree/master/RESTFul/SpeechCustom, ATT_APIPlatform_SampleApps/RESTFul/Speech/Csharp/app1/ ]

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(string.Empty+parEndPoint );

httpRequest.Headers.Add("Authorization", "Bearer " + parAccessToken);
httpRequest.Headers.Add("X-SpeechContext", parXspeechContext);
if (!string.IsNullOrEmpty(parXArgs))
{
    httpRequest.Headers.Add("X-Arg", parXArgs);
}
string contentType = this.MapContentTypeFromExtension(Path.GetExtension(parSpeechFilePath));
httpRequest.ContentLength = binaryData.Length;
httpRequest.ContentType = contentType;
httpRequest.Accept = "application/json";
httpRequest.Method = "POST";
httpRequest.KeepAlive = true;
httpRequest.SendChunked = parChunked;
postStream = httpRequest.GetRequestStream();
postStream.Write(binaryData, 0, binaryData.Length);
postStream.Close();

HttpWebResponse speechResponse = (HttpWebResponse)httpRequest.GetResponse();

推荐答案

一个简单的上传示例:

RestClient restClient = new RestClient("http://stackoverflow.com/");
RestRequest restRequest = new RestRequest("/images");
restRequest.RequestFormat = DataFormat.Json;
restRequest.Method = Method.POST;
restRequest.AddHeader("Authorization", "Authorization");
restRequest.AddHeader("Content-Type", "multipart/form-data");
restRequest.AddFile("content", imageFullPath);
var response = restClient.Execute(restRequest);

这篇关于使用 RestSharp 上传没有 Multipart/Form-Data 的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆