本文整理汇总了C#中MultipartContent类的典型用法代码示例。如果您正苦于以下问题:C# MultipartContent类的具体用法?C# MultipartContent怎么用?C# MultipartContent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MultipartContent类属于命名空间,在下文中一共展示了MultipartContent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Get
public HttpResponseMessage Get(string file1, string file2)
{
var fileA = new StreamContent(new FileStream(Path.Combine(_root, file1), FileMode.Open));
fileA.Headers.ContentType = new MediaTypeHeaderValue("text/html");
var fileB = new StreamContent(new FileStream(Path.Combine(_root, file2), FileMode.Open));
fileA.Headers.ContentType = new MediaTypeHeaderValue("text/html");
var result = new HttpResponseMessage(HttpStatusCode.OK);
var content = new MultipartContent {fileA, fileB};
result.Content = content;
return result;
}
示例2: PostNotification
public Task<HttpResponseMessage> PostNotification (BlackberryNotification notification)
{
var c = new MultipartContent ("related", Configuration.Boundary);
c.Headers.Remove("Content-Type");
c.Headers.TryAddWithoutValidation("Content-Type", "multipart/related; boundary=" + Configuration.Boundary + "; type=application/xml");
var xml = notification.ToPapXml ();
c.Add (new StringContent (xml, Encoding.UTF8, "application/xml"));
var bc = new ByteArrayContent(notification.Content.Content);
bc.Headers.Add("Content-Type", notification.Content.ContentType);
foreach (var header in notification.Content.Headers)
bc.Headers.Add(header.Key, header.Value);
c.Add(bc);
return PostAsync (Configuration.SendUrl, c);
}
示例3: PostData
public async Task<Result> PostData(Uri uri, MultipartContent header, StringContent content)
{
var httpClient = new HttpClient();
try
{
if (!string.IsNullOrEmpty(AuthenticationToken))
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
}
HttpResponseMessage response;
if (header == null)
{
if(content == null) content = new StringContent(string.Empty);
response = await httpClient.PostAsync(uri, content);
}
else
{
response = await httpClient.PostAsync(uri, header);
}
var responseContent = await response.Content.ReadAsStringAsync();
return new Result(response.IsSuccessStatusCode, responseContent);
}
catch (Exception ex)
{
throw new WebException("Kinder Chat API Error: Service error", ex);
}
}
示例4: ReadFromStreamAsyncShouldReturnUploadImageDataTest
public void ReadFromStreamAsyncShouldReturnUploadImageDataTest()
{
ImageMultipartMediaFormatter target = new ImageMultipartMediaFormatter();
Type type = typeof(UploadImageData);
Stream readStream = null;
MultipartContent content = new MultipartContent("form-data");
byte[] content_bytes=new byte[] { 10, 11, 12 };
StreamContent content_part = new StreamContent(new MemoryStream(content_bytes));
content_part.Headers.Add("Content-Disposition", @"form-data; name=fieldName; filename=image.jpg");
content_part.Headers.Add("Content-Type", "image/jpeg");
content.Add(content_part);
IFormatterLogger formatterLogger = null;
var actual = target.ReadFromStreamAsync(type, readStream, content, formatterLogger);
var actualResult = actual.Result;
Assert.IsInstanceOfType(actualResult,typeof(UploadImageData));
Assert.AreEqual(3, (actualResult as UploadImageData).ImageBuffer.Length);
for (int ind = 0; ind < 3; ind++)
{
Assert.AreEqual(content_bytes[ind], (actualResult as UploadImageData).ImageBuffer[ind]);
}
Assert.AreEqual("image.jpg", (actualResult as UploadImageData).FileName);
Assert.AreEqual("image/jpeg", (actualResult as UploadImageData).ImageType);
}
示例5: RunTest
private static async Task RunTest(string baseAddress)
{
HttpClient client = new HttpClient();
HttpRequestMessage batchRequest = new HttpRequestMessage(
HttpMethod.Post,
baseAddress + "/api/batch"
);
MultipartContent batchContent = new MultipartContent("batch");
batchRequest.Content = batchContent;
CreateBatchedRequest(baseAddress, batchContent);
using (var stdout = Console.OpenStandardOutput())
{
Console.WriteLine("<<< REQUEST >>>");
Console.WriteLine();
Console.WriteLine(batchRequest);
Console.WriteLine();
await batchContent.CopyToAsync(stdout);
Console.WriteLine();
var batchResponse = await client.SendAsync(batchRequest);
Console.WriteLine("<<< RESPONSE >>>");
Console.WriteLine();
Console.WriteLine(batchResponse);
Console.WriteLine();
await batchResponse.Content.CopyToAsync(stdout);
Console.WriteLine();
Console.WriteLine();
}
}
示例6: ExecuteAsync
public async Task ExecuteAsync(string accessToken, string ebookId, int partCount, Func<Stream, Task> streamHandler)
{
var message = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:20394/api/batch"));
var content = new MultipartContent("mixed");
message.Content = content;
for (int i = 0; i < partCount; ++i)
{
content.Add(new HttpMessageContent(
new HttpRequestMessage(HttpMethod.Get, new Uri(string.Format("http://localhost:20394/api/ebooks/ebook/{0}/part/{1}", ebookId, i)))));
}
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await client.SendAsync(message);
var streamProvider = await response.Content.ReadAsMultipartAsync();
foreach (var partialContent in streamProvider.Contents)
{
var part = await partialContent.ReadAsHttpResponseMessageAsync();
var partStream = await part.Content.ReadAsStreamAsync();
await streamHandler(partStream);
}
}
}
示例7: Main
static void Main(string[] args)
{
var client = new HttpClient();
var batchRequest = new HttpRequestMessage(
HttpMethod.Post,
"http://fsatnav:8080/api/batch"
);
var batchContent = new MultipartContent("mixed");
batchRequest.Content = batchContent;
batchContent.Add(
new HttpMessageContent(
new HttpRequestMessage(
HttpMethod.Get,
"http://localhost:8080/api/products"
)
)
);
batchContent.Add(
new HttpMessageContent(
new HttpRequestMessage(
HttpMethod.Put,
"http://localhost:8080/api/products"
)
{
Content = new StringContent("{\"Name\":\"Product X\",\"StockQuantity\":300}", Encoding.UTF8, "application/json")
}
)
);
using (Stream stdout = Console.OpenStandardOutput())
{
Console.WriteLine("<<< REQUEST >>>");
Console.WriteLine();
Console.WriteLine(batchRequest);
Console.WriteLine();
batchContent.CopyToAsync(stdout).Wait();
Console.WriteLine();
var batchResponse = client.SendAsync(batchRequest).Result;
Console.WriteLine("<<< RESPONSE >>>");
Console.WriteLine();
Console.WriteLine(batchResponse);
Console.WriteLine();
batchResponse.Content.CopyToAsync(stdout).Wait();
Console.WriteLine();
Console.WriteLine();
}
Console.ReadLine();
}
示例8: Add
public void Add()
{
var m = new MultipartContent ("a", "b");
var other = new MultipartContent ("2", "44");
other.Headers.Expires = new DateTimeOffset (2020, 11, 30, 19, 55, 22, TimeSpan.Zero);
m.Add (other);
Assert.AreEqual ("multipart/a", m.Headers.ContentType.MediaType, "#1");
Assert.AreEqual (114, m.Headers.ContentLength, "#2");
Assert.AreEqual ("--b\r\nContent-Type: multipart/2; boundary=\"44\"\r\nExpires: Mon, 30 Nov 2020 19:55:22 GMT\r\n\r\n--44\r\n\r\n--44--\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3");
Assert.AreEqual (other, m.First (), "#4");
}
示例9: Main
static void Main(string[] args)
{
var client = new HttpClient();
var batchRequest = new HttpRequestMessage(
HttpMethod.Post,
"http://localhost:52857/api/$batch"
);
var batchContent = new MultipartContent("mixed");
batchRequest.Content = batchContent;
batchContent.Add(
new HttpMessageContent(
new HttpRequestMessage(
HttpMethod.Get,
"http://localhost:52857/api/TestClassOne"
)
)
);
batchContent.Add(
new HttpMessageContent(
new HttpRequestMessage(
HttpMethod.Get,
"http://localhost:52857/api/TestClassTwo"
)
)
);
using (Stream stdout = Console.OpenStandardOutput())
{
Console.WriteLine("<<< REQUEST >>>");
Console.WriteLine();
Console.WriteLine(batchRequest);
Console.WriteLine();
batchContent.CopyToAsync(stdout).Wait();
Console.WriteLine();
var batchResponse = client.SendAsync(batchRequest).Result;
Console.WriteLine("<<< RESPONSE >>>");
Console.WriteLine();
Console.WriteLine(batchResponse);
Console.WriteLine();
batchResponse.Content.CopyToAsync(stdout).Wait();
Console.WriteLine();
Console.WriteLine();
}
Console.ReadLine();
}
示例10: BeginBatch
public void BeginBatch()
{
_batchId = Guid.NewGuid().ToString();
_changesetId = Guid.NewGuid().ToString();
this.Request = CreateRequest(CreateRequestUrl(FluentCommand.BatchLiteral));
this.Request.Method = RestVerbs.POST;
var batchContent = new MultipartContent("mixed", "batch_" + _batchId);
this.Request.Content = batchContent;
this.Request.ContentType = "application/http";
var changesetContent = new MultipartContent("mixed", "changeset_" + _changesetId);
batchContent.Add(changesetContent);
_content = changesetContent;
}
示例11: Add_2
public void Add_2()
{
var m = new MultipartContent ("a", "X");
var other = new MultipartContent ("2", "2a");
m.Add (other);
var other2 = new MultipartContent ("3", "3a");
other2.Headers.Add ("9", "9n");
m.Add (other2);
Assert.AreEqual ("multipart/a", m.Headers.ContentType.MediaType, "#1");
Assert.AreEqual (148, m.Headers.ContentLength, "#2");
Assert.AreEqual ("--X\r\nContent-Type: multipart/2; boundary=\"2a\"\r\n\r\n--2a\r\n\r\n--2a--\r\n\r\n--X\r\nContent-Type: multipart/3; boundary=\"3a\"\r\n9: 9n\r\n\r\n--3a\r\n\r\n--3a--\r\n\r\n--X--\r\n",
m.ReadAsStringAsync ().Result, "#3");
Assert.AreEqual (other, m.First (), "#4");
}
示例12: PostConvertFile_ConvertToKml_ShouldReturnByteArray
public void PostConvertFile_ConvertToKml_ShouldReturnByteArray()
{
var multipartContent = new MultipartContent();
var streamContent = new StreamContent(new MemoryStream(new byte[1] { 1 }));
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"files\"",
FileName = "\"SomeFile.twl\""
};
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/kml");
multipartContent.Add(streamContent);
_controller.Request = new HttpRequestMessage();
_controller.Request.Content = multipartContent;
_gpsBabelGateway.ConvertFileFromat(Arg.Any<byte[]>(), "naviguide", "kml").Returns(Task.FromResult(new byte[2] { 1, 1 }));
var response = _controller.PostConvertFile("kml").Result as OkNegotiatedContentResult<byte[]>;
Assert.AreEqual(2, response.Content.Length);
}
示例13: Main
static void Main()
{
var address = "http://localhost:9000/";
using (WebApp.Start<Startup>(address))
{
var client = new HttpClient();
var batchContent = new MultipartContent("mixed")
{
new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, address + "/api/items")
{
Content = new ObjectContent(typeof (Item),
new Item {Country = "Switzerland", Id = 1, Name = "Filip"},
new JsonMediaTypeFormatter())
}),
new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, address + "/api/items")
{
Content =
new ObjectContent(typeof (Item), new Item {Country = "Canada", Id = 2, Name = "Felix"},
new JsonMediaTypeFormatter())
}),
new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, address + "/api/items"))
};
var batchRequest = new HttpRequestMessage(HttpMethod.Post, address + "/api/batch")
{
Content = batchContent
};
var batchResponse = client.SendAsync(batchRequest).Result;
var streamProvider = batchResponse.Content.ReadAsMultipartAsync().Result;
foreach (var content in streamProvider.Contents)
{
var response = content.ReadAsHttpResponseMessageAsync().Result;
Print(response);
}
Console.ReadLine();
}
}
示例14: Ctor
public void Ctor ()
{
using (var m = new MultipartContent ("a", "b")) {
m.Headers.Add ("extra", "value");
Assert.AreEqual ("multipart/a", m.Headers.ContentType.MediaType, "#1");
Assert.AreEqual (14, m.Headers.ContentLength, "#2");
Assert.AreEqual ("--b\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3");
}
using (var m = new MultipartContent ()) {
Assert.AreEqual ("multipart/mixed", m.Headers.ContentType.MediaType, "#11");
Assert.AreEqual (84, m.Headers.ContentLength, "#12");
}
using (var m = new MultipartContent ("ggg")) {
Assert.AreEqual ("multipart/ggg", m.Headers.ContentType.MediaType, "#21");
Assert.AreEqual (84, m.Headers.ContentLength, "#22");
}
}
示例15: Build
public HttpContent Build(string boundary)
{
if (boundary == null)
{
throw new ArgumentNullException("boundary");
}
if (string.IsNullOrWhiteSpace(boundary))
{
throw new ArgumentException("The provided boundary value is invalid", "boundary");
}
MultipartContent content = new MultipartContent("mixed", boundary);
foreach (var request in Requests)
{
HttpMessageContent messageContent = new HttpMessageContent(request);
messageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/http");
messageContent.Headers.Add("Content-Transfer-Encoding", "binary");
content.Add(messageContent);
}
return content;
}