本文整理汇总了C#中StringContent类的典型用法代码示例。如果您正苦于以下问题:C# StringContent类的具体用法?C# StringContent怎么用?C# StringContent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StringContent类属于命名空间,在下文中一共展示了StringContent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TelemetryIngest
public async Task<bool> TelemetryIngest(Telemetry telemetry)
{
string serviceBusNamespace = "iotmc-ns";
string serviceBusUri = string.Format("{0}.servicebus.windows.net", serviceBusNamespace);
string eventHubName = "IoTMC";
string eventHubSASKeyName = "Device01";
string eventHubSASKey = "<< Your SAS Key here >>";
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(String.Format("https://{0}", serviceBusUri));
httpClient.DefaultRequestHeaders.Accept.Clear();
string sBToken = CreateServiceBusSASToken(eventHubSASKeyName, eventHubSASKey, serviceBusUri);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedAccessSignature", sBToken);
HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(telemetry), Encoding.UTF8);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
string ingestPath = String.Format("/{0}/publishers/device01/messages", eventHubName);
var response = await httpClient.PostAsync(ingestPath, httpContent);
if (response.IsSuccessStatusCode)
{
return true;
}
return false;
}
}
示例2: Ctor_EmptyString_Accept
public async Task Ctor_EmptyString_Accept()
{
// Consider empty strings like null strings (null and empty strings should be treated equally).
var content = new StringContent(string.Empty);
Stream result = await content.ReadAsStreamAsync();
Assert.Equal(0, result.Length);
}
示例3: ReadDeeplyNestedObjectThrows
public void ReadDeeplyNestedObjectThrows()
{
FormUrlEncodedMediaTypeFormatter formatter = new FormUrlEncodedMediaTypeFormatter() { MaxDepth = 100 };
StringContent content = new StringContent(GetDeeplyNestedObject(125));
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
Assert.ThrowsArgument(
() => formatter.ReadFromStreamAsync(typeof(JToken), content.ReadAsStreamAsync().Result, content.Headers, null).Result,
null);
}
示例4: PostCallAsync
public static async Task<string> PostCallAsync(string url, string parameters)
{
var content = new StringContent(parameters);
string output = string.Empty;
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.PostAsync(url, content);
output = await response.Content.ReadAsStringAsync();
}
return output;
}
示例5: GivenSomeContentWithNoBadPhrases_PhrasesThatExist_ReturnsANullList
public void GivenSomeContentWithNoBadPhrases_PhrasesThatExist_ReturnsANullList()
{
// Arrange.
const string content =
"foo 1 12l3j1kl;j434;5 3klkrj 987*&^897^DF hsfd l;ajf;o2u3r8fjdasf jdsajf ;aljsfj asf jas;df 8asuf 80sdf 9d (& 8H ";
var stringContent = new StringContent(PhraseList);
// Act.
var results = stringContent.PhrasesThatExist(content);
// Assert.
Assert.Null(results);
}
示例6: PapRequestMessage
/// <summary>
/// Initializes a new instance of the <see cref="PapRequestMessage"/>
/// class with both control and content parts.
/// </summary>
/// <param name="control">The control.</param>
/// <param name="content">The content.</param>
public PapRequestMessage(PushMessageControl control, StringContent content)
{
_control = control;
_content = content;
BoundaryGenerator bg = new BoundaryGenerator();
Boundary = bg.MakeBoundary();
if (content != null && control != null)
{
content.PushId = control.PushId;
}
}
示例7: Dispose_DisposesInnerContent
public void Dispose_DisposesInnerContent()
{
// Arrange
StringContent innerContent = new StringContent("HelloWorld!");
HttpRequestMessage request = new HttpRequestMessage();
ProgressMessageHandler progressHandler = new ProgressMessageHandler();
ProgressContent progressContent = new ProgressContent(innerContent, progressHandler, request);
// Act
progressContent.Dispose();
// Assert
Assert.ThrowsObjectDisposed(() => innerContent.LoadIntoBufferAsync(), typeof(StringContent).FullName);
}
示例8: GetDataUsingDataContract
public async Task<CompositeType> GetDataUsingDataContract(CompositeType composite)
{
var soapString = this.CreateGetDataUsingDataContractRequest(composite);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("SOAPAction", "http://tempuri.org/IService1/GetDataUsingDataContract");
var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
using (var response = await client.PostAsync(this.uri, content))
{
string soapResponse = await response.Content.ReadAsStringAsync();
CompositeType result = this.ParseGetDataUsingDataContractResponse(soapResponse);
return result;
}
}
}
示例9: Constructor_CopyHeadersFromInnerContent
public void Constructor_CopyHeadersFromInnerContent()
{
// Arrange
StringContent innerContent = new StringContent("HelloWorld!");
innerContent.Headers.Add(TestHeader, TestValue);
HttpRequestMessage request = new HttpRequestMessage();
ProgressMessageHandler progressHandler = new ProgressMessageHandler();
// Act
ProgressContent progressContent = new ProgressContent(innerContent, progressHandler, request);
// Assert
ValidateContentHeader(progressContent);
Assert.Equal(innerContent.Headers.ContentType, progressContent.Headers.ContentType);
Assert.Equal(innerContent.Headers.ContentLength, progressContent.Headers.ContentLength);
}
示例10: GivenSomeContentWithBadPhrases_PhrasesThatExist_ReturnsAListOfBadPhrasesFound
public void GivenSomeContentWithBadPhrases_PhrasesThatExist_ReturnsAListOfBadPhrasesFound()
{
// Arrange.
const string content =
"This is a bad review. I noticed that there were a lot of white people living in Ivanhoe. This doesn't mean this is a racist suburb, though.";
var stringContent = new StringContent(PhraseList);
// Act.
var results = stringContent.PhrasesThatExist(content);
// Assert.
Assert.NotNull(results);
Assert.NotEmpty(results);
Assert.Equal(2, results.Count);
}
示例11: Ctor_UseCustomEncodingAndMediaType_EncodingUsedAndContentTypeHeaderUpdated
public async Task Ctor_UseCustomEncodingAndMediaType_EncodingUsedAndContentTypeHeaderUpdated()
{
// Use UTF-8 encoding to serialize a chinese string.
string sourceString = "\u4f1a\u5458\u670d\u52a1";
var content = new StringContent(sourceString, Encoding.UTF8, "application/custom");
Assert.Equal("application/custom", content.Headers.ContentType.MediaType);
Assert.Equal("utf-8", content.Headers.ContentType.CharSet);
var destination = new MemoryStream(12);
await content.CopyToAsync(destination);
string destinationString = Encoding.UTF8.GetString(destination.ToArray(), 0, (int)destination.Length);
Assert.Equal(sourceString, destinationString);
}
示例12: GivenSomeContentWithBadPhrasesSuppliedSeparately_PhrasesThatExist_ReturnsAListOfBadPhrasesFound
public void GivenSomeContentWithBadPhrasesSuppliedSeparately_PhrasesThatExist_ReturnsAListOfBadPhrasesFound()
{
// Arrange.
const string content =
"This is a bad review. &^*(sd79ad hskah sad (*&(&97s a9 I noticed adult living that there were a lot of white people living in Ivanhoe. This doesn't mean this is a racist suburb, though.";
var stringContent = new StringContent();
// Act.
var results = stringContent.PhrasesThatExist(content, PhraseList);
// Assert.
Assert.NotNull(results);
Assert.NotEmpty(results);
Assert.Equal(3, results.Count);
Assert.Equal("adult living", results.First().Phrase);
Assert.Equal(65, results.First().IndexOn);
}
示例13: Ctor_DefineNoEncoding_DefaultEncodingUsed
public async Task Ctor_DefineNoEncoding_DefaultEncodingUsed()
{
string sourceString = "\u00C4\u00E4\u00FC\u00DC";
var content = new StringContent(sourceString);
Encoding defaultStringEncoding = Encoding.GetEncoding("utf-8");
// If no encoding is defined, the default encoding is used: utf-8
Assert.Equal("text/plain", content.Headers.ContentType.MediaType);
Assert.Equal(defaultStringEncoding.WebName, content.Headers.ContentType.CharSet);
// Make sure the default encoding is also used when serializing the content.
var destination = new MemoryStream();
await content.CopyToAsync(destination);
Assert.Equal(8, destination.Length);
destination.Seek(0, SeekOrigin.Begin);
string roundTrip = new StreamReader(destination, defaultStringEncoding).ReadToEnd();
Assert.Equal(sourceString, roundTrip);
}
示例14: SendAsync_DoesNotInsertSendProgressWithoutEntityOrHandlerPresent
public Task SendAsync_DoesNotInsertSendProgressWithoutEntityOrHandlerPresent(bool insertRequestEntity, bool addSendProgressHandler)
{
// Arrange
HttpMessageInvoker invoker = CreateMessageInvoker(includeResponseEntity: false, addReceiveProgressHandler: false, addSendProgressHandler: addSendProgressHandler);
HttpRequestMessage request = new HttpRequestMessage();
HttpContent content = null;
if (insertRequestEntity)
{
content = new StringContent("Request Entity!");
content.Headers.Add(TestHeader, TestValue);
request.Content = content;
}
// Act
return invoker.SendAsync(request, CancellationToken.None).ContinueWith(
task =>
{
// Assert
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
if (insertRequestEntity && addSendProgressHandler)
{
ValidateContentHeader(request.Content);
Assert.NotSame(content, request.Content);
Assert.IsType<ProgressContent>(request.Content);
}
else
{
if (insertRequestEntity)
{
Assert.IsType<StringContent>(request.Content);
}
else
{
Assert.Null(request.Content);
}
}
});
}
示例15: ReverseOrdinalCompareTo
// content.chars <=> this.chars
public override int ReverseOrdinalCompareTo(StringContent/*!*/ content) {
return content.OrdinalCompareTo(_data);
}