2016-08-07 2 views
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에 인코딩 된 메시지가 있는데, 나는 그것이 오류를 던지고있는 이유라고 생각하지만 읽는 법을 모르겠습니다. 어떻게 charset을 설정합니까?

+0

'public static async void'를'public static async Task'로 변경하고 오류가 중지되는지 확인하십시오. 'async void' 사용을 피해야합니다. – Nkosi

+0

@Nkosi 이것은 예외를 변경하지 않습니다. 여전히 인코딩 유형이 마음에 들지 않습니다. – Guerrilla

+0

전송중인 요청을 스캔하고 'ContentType' 헤더를 확인하십시오. "ISO-8859-1"은 '지원되는 인코딩 이름이 아닙니다'당신은 지원되지 않는 인코딩을 사용하고있는 것 같습니다 – Nkosi

답변

0

액세스하고있는 API에서 필드가 반환 유형을 지정할 수 있습니다. 양식 필드에 Json을 지정한 다음 작동했습니다.