本文整理汇总了C#中System.Net.Http.FormUrlEncodedContent.LoadIntoBufferAsync方法的典型用法代码示例。如果您正苦于以下问题:C# FormUrlEncodedContent.LoadIntoBufferAsync方法的具体用法?C# FormUrlEncodedContent.LoadIntoBufferAsync怎么用?C# FormUrlEncodedContent.LoadIntoBufferAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.FormUrlEncodedContent
的用法示例。
在下文中一共展示了FormUrlEncodedContent.LoadIntoBufferAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendBaidu
public static async Task<string> SendBaidu(string httpMethod, string url, string secretKey, Baidu_Mod mod)
{
//获取签名
string strSign = CreateSign(httpMethod, url, secretKey, mod);
string responseContent = ""; //返回字符串
//使用本机代理fiddler进行测试
//var handler = new HttpClientHandler()
//{
// Proxy = new WebProxy("http://127.0.0.1:8888", false, new string[] { }),
// UseProxy = true
//};
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url); //设置WebAPI的地址+类+方法
client.DefaultRequestHeaders.Accept.Clear(); //清空缓存
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"){CharSet="utf-8"}); //返回值为json和utf-8编码
//响应消息
HttpResponseMessage response = null;
//判断使用不同的HttpMethod,生成不同的参数,获取相关返回数据
if (HttpMethod.Get.Method.ToLower().Equals(httpMethod.ToLower())) //get方法(暂不提供)
{
}
else if (HttpMethod.Post.Method.ToLower().Equals(httpMethod.ToLower())) //post方法
{
//生成post的参数,并且设置charset为utf-8
Dictionary<string, string> requestDictionary = GetParamerCollection(mod, strSign);
var contents = new FormUrlEncodedContent(requestDictionary);
contents.Headers.ContentType.CharSet = "utf-8";
await contents.LoadIntoBufferAsync();
response = await client.PostAsync(url, contents);
}
//返回值
responseContent = await response.Content.ReadAsStringAsync();
}// http完结
return responseContent;
}
示例2: SendBaidu
public static async Task<string> SendBaidu(string httpMethod, string url, string secretKey, Baidu_Mod mod)
{
//获取签名
string strSign = CreateSign(httpMethod, url, secretKey, mod);
string responseContent = ""; //返回字符串
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url); //设置WebAPI的地址+类+方法
client.DefaultRequestHeaders.Accept.Clear(); //清空缓存
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json") { CharSet = "utf-8" }); //返回值为json和utf-8编码
//响应消息
HttpResponseMessage response = null;
//判断使用不同的HttpMethod,生成不同的参数,获取相关返回数据
if (HttpMethod.Get.Method.ToLower().Equals(httpMethod.ToLower())) //get方法(暂不提供)
{
}
else if (HttpMethod.Post.Method.ToLower().Equals(httpMethod.ToLower())) //post方法
{
//生成post的参数,并且设置charset为utf-8
Dictionary<string, string> requestDictionary = GetParamerCollection(mod, strSign);
var contents = new FormUrlEncodedContent(requestDictionary);
contents.Headers.ContentType.CharSet = "utf-8";
await contents.LoadIntoBufferAsync();
try
{
response = await client.PostAsync(url, contents);
//返回值
responseContent = await response.Content.ReadAsStringAsync();
}
catch (WebException ex)
{
Stream stream = ex.Response.GetResponseStream();
string m = ex.Response.Headers.ToString();
byte[] buf = new byte[256];
stream.Read(buf, 0, 256);
stream.Close();
int count = 0;
foreach (var b in buf)
{
if (b > 0)
{
count++;
}
else
{
break;
}
}
responseContent = "Post:" + url + ex.Message + "\r\n\r\n" + Encoding.UTF8.GetString(buf, 0, count);
}
}
}
return responseContent;
}