簡體   English   中英

如何將application / x-www-form-urlencoded轉換為JSON?

[英]How to convert application/x-www-form-urlencoded to JSON?

我使用Httpclient.PostRequest發出了內容類型為application/x-www-form-urlencoded UTF-8的http發布請求。 我需要將application/x-www-form-urlencoded請求結果轉換為json字符串。 如何在C#中將application/x-www-form-urlencoded為json?

我做了下一個職位要求。

HttpResponseMessage response;
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://sv.ki.court.gov.ua/new.php"))
{
   request.Headers.TryAddWithoutValidation("Connection", "keep-alive");
   request.Headers.TryAddWithoutValidation("Accept", "application/json, text/javascript, */*; q=0.01");
   request.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br");
   request.Headers.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.9");
   request.Headers.TryAddWithoutValidation("Referer", "https://sv.ki.court.gov.ua/sud2608/gromadyanam/csz");
   request.Headers.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
   request.Headers.TryAddWithoutValidation("Origin", "https://sv.ki.court.gov.ua");
   request.Headers.TryAddWithoutValidation("X-Requested-With", "XMLHttpRequest");
   request.Headers.TryAddWithoutValidation("Cookie", "_ga=GA1.3.1754947237.1562858299; PHPSESSID=1nosm5dhdljsv8tpsu97bl8dn5; cookiesession1=257F9822DPOYFGLLUDVTJCMDYYR98AB6; _gid=GA1.3.1599643655.1563891940; _gat=1");

   request.Content = new StringContent("q_court_id=2608", Encoding.UTF8, "application/x-www-form-urlencoded");

   response = await _client.SendAsync(request);
}

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var json = await response.Content.ReadAsStringAsync();

作為StringContent類型,我使用了application/x-www-form-urlencoded application/json不起作用。 發布請求返回結果字符串“ \<.... ”。 我需要將響應結果application/x-www-form-urlencodedapplication/json

這應該給您一些想法。 在以下PostAsync內部的示例中,您傳入一個StringContent的新實例,您在其中設置內容的類型,在本例中為Json

public static async Task MainAsync()
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:1234");
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("", "Joe Bloggs")
        });
        var result = await client.PostAsync("/api/Customer/exists", new StringContent(content.ToString(), Encoding.UTF8, "application/json"));
        string resultContent = await result.Content.ReadAsStringAsync();
        Console.WriteLine(resultContent);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM