我正在尝试使用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
中有一条编码消息,我猜这是它抛出错误的原因,但我不知道如何阅读。如何设置字符集?
答案 0 :(得分:0)
我正在访问的API允许字段指定返回类型。我在表单字段中指定了Json,然后就可以了。