使用.NET 4.5(或通过从NuGet
添加Microsoft.Net.Http包的.NET 4.0 ),可以模拟表单请求。这是一个例子:
private async Task<System.IO.Stream> Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes){ HttpContent stringContent = new StringContent(paramString); HttpContent fileStreamContent = new StreamContent(paramFileStream); HttpContent bytesContent = new ByteArrayContent(paramFileBytes); using (var client = new HttpClient()) using (var formData = new MultipartFormDataContent()) { formData.Add(stringContent, "param1", "param1"); formData.Add(fileStreamContent, "file1", "file1"); formData.Add(bytesContent, "file2", "file2"); var response = await client.PostAsync(actionUrl, formData); if (!response.IsSuccessStatusCode) { return null; } return await response.Content.ReadAsStreamAsync(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)