C# HttpClient FormUrlEncodedContent 编码 (VS 2012) [英] C# HttpClient FormUrlEncodedContent Encoding (VS 2012)

查看:22
本文介绍了C# HttpClient FormUrlEncodedContent 编码 (VS 2012)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 HttpClient.我正在使用网络表单参数发布.其中一个值(不是名称)是外来瑞典字符 ö ,#246;ö ASCII:拉丁小写字母 O 变音

I'm using the HttpClient. I'm posting with web form parameters. One of the values (not name) is a foreign Swedish character ö , #246; ö ASCII: Latin Small Letter O Umlaut

手动,IE、Firefox 和 Chrome 都将此字符转换为 S%F6k,一切正常.然而,VS 2012 C# 版本将它(通过 FormUrlEncodedContent(dict))转换为 %C3%B6

Manually, IE, Firefox and Chrome all convert this character to S%F6k and everything works fine. However VS 2012 C# release converts it (via FormUrlEncodedContent(dict)) to %C3%B6

有没有办法告诉 VS 2012 将其转换为友好的 S%F6k(并且仍然使用 HttpClient)?

Is there a way to tell VS 2012 to convert it, to the friendly S%F6k (and still use HttpClient)?

我附上了大部分代码,可能对其他人有帮助(cookies、代理等...)

I've attached most of the code, which may help others (cookies, proxy, etc...)

// Create Handler
var handler = new HttpClientHandler();

// Cookies
var cc = new CookieContainer();
handler.CookieContainer = cc;

// Proxy - for fiddler
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://localhost:8888");
handler.Proxy = proxy;

// Create the client
var client = new HttpClient(handler);

var request4 = new HttpRequestMessage();

client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, */*");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.8,sv-SE;q=0.5,sv;q=0.3");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

// Form Data
var dict4 = new Dictionary<string, string>
{
    { "page", "kantlista" },
    { "kod", "A0004n" },
    { "termin", "H12" },
    { "anmkod", "17113" },
    { "urval", "ant" },
    { "listVal", "namn" },
    { "method", "Sök" } // S%F6k
}; // dict

request4.Content = new FormUrlEncodedContent(dict4);

var value4 = new FormUrlEncodedContent(dict4);
string uri4 = "https://www.ltu.se/ideal/ListaKursant.do";
var response4 = await client.PostAsync(uri4, value4);
response4.Headers.Add("Cache-Control", "no-cache")
response4.EnsureSuccessStatusCode();
string responseBody4 = await response4.Content.ReadAsStringAsync();

推荐答案

FormUrlEncodedContent 类以 utf8 编码对表单数据进行编码.
试试 ByteArrayContent 类和 HttpUtility.UrlEncode(String, Encoding) 进行编码.

这篇关于C# HttpClient FormUrlEncodedContent 编码 (VS 2012)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆