0
我想使用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
编码的消息,我猜是它抛出错误的原因,但我不知道如何阅读。如何设置字符集?
将'public static async void'改为'public static async Task'并查看错误是否停止。你应该避免使用'async void' – Nkosi
@Nkosi这不会改变异常。它仍然不喜欢编码类型 – Guerrilla
扫描发送的请求并确认“ContentType”标题。基于错误消息'“ISO-8859-1”'不是受支持的编码名称,你似乎正在使用不受支持的编码 – Nkosi