我正在一個ASP.Net MVC 4 Web應用程序,我需要將JSON對象發佈到第三方API。現在我需要將我的JSON數據編碼,如API文檔中提到的那樣,將內容類型設置爲application/x-www-form-urlencoded
。所以我嘗試通過指定Content-Type
爲wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
如下: -無法編碼我的JSON對象insdie我的WebClient UploadString使用頭[[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);
}
,但仍是數據沒有被編碼..如果我發送一個值,如123%456
我的JSON字符串內,它將被保存爲123E6t
在第三部分系統中。現在作爲解決方法,我已經手動編碼JSON對象使用WebUtility.UrlEncode(data)
之前發送到第三部分API,我可以看到,值如123%456
將被正確保存... 但我不想手動編碼數據,,我想設置內容類型來指定編碼...這可能嗎?
我看你要張貼JSON數據,不應該是'的contentType = 「應用/ JSON」'? – Turbot
@Turbot,但我想編碼我的json對象內的值???這可能嗎 ? –
您是否嘗試設置您的請求'Content-Type = application/json'來查看它是否有效? – Turbot