簡體   English   中英

有沒有辦法在 C# 中傳遞這個內容類型“application/x-www-form-urlencoded;v=2.0”?

[英]Is there a way to pass this contenttype "application/x-www-form-urlencoded;v=2.0" in C#?

我試圖在 C# 中復制 CURL 中的以下代碼。 請注意,此代碼與 curl 一起正常工作。 我正在使用這個https://curl.olsh.me/創建請求。

curl -k -u "默認用戶:機器人" -H "內容類型:application/x-www-form-urlencoded;v=2.0" -d "value=TRUE" " https://localhost/rw/rapid/符號/RAPID/T_ROB1/EGMDemo/run_egm/data?mastership=implicit

我試圖將此命令轉換為 c# 並在到達該行時:

request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded;v=2.0");

我收到一個異常,即application/x-www-form-urlencoded;v=2.0無效。 如何傳遞此內容類型? 如果使用 httpclient 無法實現,還有其他方法可以做到嗎?


    var handler = new HttpClientHandler();
    handler.ServerCertificateCustomValidationCallback = (requestMessage, certificate, chain, policyErrors) => true; 

    using (var httpClient = new HttpClient(handler))
    {
        using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://localhost/rw/rapid/symbol/RAPID/T_ROB1/EGMDemo/run_egm/data?mastership=implicit"))
        {
            var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("Default User:robotics"));
            request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}"); 

            request.Content = new StringContent("value=TRUE");
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded;v=2.0"); 

            var response = await httpClient.SendAsync(request);
        }
    }


可以嘗試直接使用request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded;v=2.0");

MediaTypeHeaderValue僅支持為 HTTP/1.1 定義的標准媒體類型

編輯錯誤:)

暫無
暫無

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

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