Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文件发送到端点RESTful C#?_C#_Wpf_Winforms_C# 4.0 - Fatal编程技术网

如何将文件发送到端点RESTful C#?

如何将文件发送到端点RESTful C#?,c#,wpf,winforms,c#-4.0,C#,Wpf,Winforms,C# 4.0,要将数据发送到服务器,我使用以下解决方案: 但是如何通过POST将数据发送到concret end-point RESTful?我就是这样做的-对于补丁也同样有效。下面的代码经过优化,具有有限的注释和异常处理,以演示该原理 该示例是一个通用异步方法,因此它可以接受任何可序列化的内容,包括单个和多个文件流: /// <summary> /// Calls a JSON/REST POST service. /// </summary> /// <typeparam

要将数据发送到服务器,我使用以下解决方案:


但是如何通过POST将数据发送到concret end-point RESTful?

我就是这样做的-对于
补丁
也同样有效。下面的代码经过优化,具有有限的注释和异常处理,以演示该原理

该示例是一个通用异步方法,因此它可以接受任何可序列化的内容,包括单个和多个文件流:

/// <summary>
/// Calls a JSON/REST POST service.
/// </summary>
/// <typeparam name="TValue">Type of to be sent value.</typeparam>
/// <param name="loadPackage">Mandatory. The package the post call shall carry.</param>
/// <param name="requestUri">Mandatory. URI which shall be called.</param>
/// <returns>Returns the plain service response.</returns>
public async Task<HttpResponseMessage> CallPostServicePlainAsync<TValue>(
    TValue loadPackage,
    string requestUri)
{
    using (var httpClient = CreateHttpClient()) // or just `new HttpClient()` plus magic
    {
        bool isStream = typeof(TValue) == typeof(Stream);
        bool isMultipleStreams = typeof(TValue) == typeof(Stream[]);
        if (isStream || isMultipleStreams)
        {
            var message = new HttpRequestMessage();
            message.Method = HttpMethod.Post; // that's what makes it a POST :-)
            var content = new MultipartFormDataContent();
            if (isStream) // single stream
                content.Add(new StreamContent(loadPackage as Stream));
            else if (isMultipleStreams) // this is an array of streams
                foreach (Stream stream in loadPackage as Stream[])
                    content.Add(new StreamContent(stream));
            else // oops
                throw new NotImplementedException("incorrect load package.");
            message.Content = content;
            message.RequestUri = new Uri(requestUri);
            return await httpClient.SendAsync(message).ConfigureAwait(false);
        } else {
            // standard serializable content (not streams)
            ...
        }
    }
}
//
///调用JSON/REST POST服务。
/// 
///要发送的值的类型。
///强制性的。邮递员应携带的包裹。
///强制性的。应调用的URI。
///返回普通服务响应。
公共异步任务CallPostServicePlainAsync(
TValue loadPackage,
字符串(URI)
{
使用(var httpClient=CreateHttpClient())//或只使用'newhttpclient()'加上magic
{
bool isStream=typeof(TValue)==typeof(Stream);
bool isMultipleStreams=typeof(TValue)==typeof(Stream[]);
if(isStream | | isMultipleStreams)
{
var message=new-HttpRequestMessage();
message.Method=HttpMethod.Post;//这就是它成为Post的原因:-)
var content=新的MultipartFormDataContent();
if(isStream)//单流
添加(新的StreamContent(作为流的loadPackage));
else if(isMultipleStreams)//这是一个流数组
foreach(loadPackage中的流作为流[])
添加(新的流内容(流));
否则//oops
抛出新的NotImplementedException(“错误的加载包”);
message.Content=内容;
message.RequestUri=新Uri(RequestUri);
返回wait-httpClient.sendaync(message).configurewait(false);
}否则{
//标准可序列化内容(非流)
...
}
}
}
您所说的“restful”是什么意思?RESTAPI只是HTTP端点,REST部分不会更改底层HTTP请求的语义。