在过帐表单时设置正确的编码类型

本文关键字:编码 类型 设置 表单 | 更新日期: 2023-09-27 18:01:09

我正在尝试使用HttpClient发布一篇文章。这就是我尝试过的:

    public static async void DoPost(string url, string user, string password)
    {
        List<KeyValuePair<string, string>> postValues = new List<KeyValuePair<string, string>>();
        postValues.Add(new KeyValuePair<string, string>("method", "login"));
        postValues.Add(new KeyValuePair<string, string>("user", user));
        postValues.Add(new KeyValuePair<string, string>("pass", password));
        FormUrlEncodedContent formEnc = new FormUrlEncodedContent(postValues);
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await client.PostAsync(url, formEnc))
        using (HttpContent content = response.Content)
        {
            // ... Read the string.
            string result = await content.ReadAsStringAsync();
            // ... Display the result.
            if (result != null &&
            result.Length >= 50)
            {
                Console.WriteLine(result.Substring(0, 50) + "...");
            }
        }
    }

它因没有正确的编码类型而引发异常。我不确定编码类型应该是什么或者在哪里设置。这是一个例外:

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.
  Source=System.Net.Http
  StackTrace:
       at System.Net.Http.HttpContent.ReadBufferedContentAsString()
       at System.Net.Http.HttpContent.<>c.<ReadAsStringAsync>b__36_0(HttpContent s)
       at System.Net.Http.HttpContent.<WaitAndReturnAsync>d__62`2.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at SuiteCrmTest.Program.<DoPost>d__1.MoveNext() in D:'Projects'SuiteCrmTest'src'SuiteCrmTest'Program.cs:line 34
  InnerException: 
       HResult=-2147024809
       Message='"ISO-8859-1"' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
Parameter name: name
       ParamName=name
       Source=System.Private.CoreLib
       StackTrace:
            at System.Globalization.EncodingTable.internalGetCodePageFromName(String name)
            at System.Globalization.EncodingTable.GetCodePageFromName(String name)
            at System.Text.Encoding.GetEncoding(String name)
            at System.Net.Http.HttpContent.ReadBufferedContentAsString()
       InnerException: 

如何设置正确的内容类型?

编辑:

收到的标头:

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 07 Aug 2016 23:46:17 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.17
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip

HTTP/1.1 500 Internal Server Error
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 07 Aug 2016 23:46:26 GMT
Content-Type: text/html; charset="ISO-8859-1"
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.17

我认为第一个标题是导致问题的原因。我认为第二个标题是重定向到html消息。在第一个标头上,ISO-8859-1中有一条编码消息,我想这就是它抛出错误的原因,但我不知道如何读取。如何设置字符集?

在过帐表单时设置正确的编码类型

我访问的API允许一个字段指定返回类型。我在表单字段中指定了Json,然后它就工作了。