无法使用Headers ["Content-Type"] ="application/x-www-form-urlencoded"在我的WebClient UploadString中编码我的JSON对象. [英] Unable to encode my JSON object inside my WebClient UploadString using Headers["Content-Type"] = "application/x-www-form-urlencoded"

查看:214
本文介绍了无法使用Headers ["Content-Type"] ="application/x-www-form-urlencoded"在我的WebClient UploadString中编码我的JSON对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.Net MVC 4 Web应用程序,并且需要将JSON对象发布到第三部分API.现在,我需要通过将内容类型设置为application/x-www-form-urlencoded来对JSON数据进行编码,如API文档中所述.所以我尝试通过将Content-Type指定为wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";来尝试以下操作:

I am working on an ASP.Net MVC 4 web application and I need to post a JSON object to a 3rd part API. Now I need my JSON data to be encoded by setting the content type to be application/x-www-form-urlencoded as mentioned inside the API documentations. so I tried the following by specifying the Content-Type as wc.Headers["Content-Type"] = "application/x-www-form-urlencoded"; :

var data = JsonConvert.SerializeObject(mainresourceinfo); 
using (WebClient wc = new WebClient()) 
{
    string url = currentURL + "resources?AUTHTOKEN=" + pmtoken;
    Uri uri = new Uri(url);

    //  wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";

    crudoutput = wc.UploadString(uri, "INPUT_DATA=" + data);
}

,但数据仍未编码..如果我在JSON字符串中发送诸如123%456之类的值,它将在第三部分系统中另存为123E6t.现在作为一种解决方法,我已经使用WebUtility.UrlEncode(data)手动编码了JSON对象,然后将其发送到第三部分API,并且我可以看到诸如123%456之类的值将被正确保存... 但是我不想手动编码数据,我想设置内容类型以指定编码...这可能吗?

but still the data is not being encoded .. and if I send a value such as 123%456 inside my JSON string, it will be saved as 123E6t inside the 3rd part system. now as a workaround i have manually encoded the JSON object using WebUtility.UrlEncode(data) before sending it to the 3rd part API, and I can see that the values such as 123%456 will be saved correctly ... but I do not want to be manually encoding the data ,, I want to set the content type to specify the encoding ...is this possible?

推荐答案

使用

wc.Headers["Content-Type"] = "application/json"

并尝试设置字符集

wc.Encoding = Encoding.UTF8;
crudoutput = wc.UploadString(uri, "INPUT_DATA=" + Encoding.UTF8.GetString(data));

Charset指定文档的字符编码. HTTP 1.1的默认字符集是ANSI,因为ANSI与 ISO-8859-1 相同.
默认情况下,其他WebClient.Encoding设置为Encoding.Default,这意味着您将获得ANSI代码页.所有默认"编码都会丢失数据,因此,您可以改用UTF-8.

Charset is specifying the character encoding of the document. Default charset for HTTP 1.1 is ANSI because ANSI is identical to ISO-8859-1.
Alse WebClient.Encoding by default is set to Encoding.Default that means you get ANSI code page. All Default encodings lose data, so, you might use UTF-8 instead.

因此,如果设置字符集,并不意味着所有内容都必须编码为Unicode,而是意味着这些文档只能包含Unicode定义的字符.这意味着您还需要将内容另存为UTF-8.在这种情况下,您可以使用Encoding.UTF8.GetString()

So, if you set charset it does not mean that all content have to be encoded as Unicode, but it does mean that these documents can only contain characters defined by Unicode. It means you also need to save your content as UTF-8. In that case you can use Encoding.UTF8.GetString()

这篇关于无法使用Headers ["Content-Type"] ="application/x-www-form-urlencoded"在我的WebClient UploadString中编码我的JSON对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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