2015-01-21 67 views
0

及彼的文字是我的代碼:從Web請求

  Uri myUri = new Uri("my url"); 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create (myUri); 

      // Set some reasonable limits on resources used by this request 
      request.MaximumAutomaticRedirections = 4; 
      request.MaximumResponseHeadersLength = 4; 
      // Set credentials to use for this request. 
      request.Credentials = CredentialCache.DefaultCredentials; 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      Console.WriteLine ("Content length is {0}", response.ContentLength); 
      Console.WriteLine ("Content type is {0}", response.ContentType); 

      // Get the stream associated with the response. 
      Stream receiveStream = response.GetResponseStream(); 

      // Pipes the stream to a higher level stream reader with the required encoding format. 
      StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8); 

      Console.WriteLine ("Response stream received."); 
      Console.WriteLine (readStream.ReadToEnd()); 
      response.Close(); 
      readStream.Close(); 

的反應應該是這樣的:

[ 
    { 
     "book_name": "CYRILLIC LETTERS", 
     "book_id": "Text", 
    }, 
    { 
     "book_name": "CYRILLIC LETTERS", 
     "book_id": "Text", 
    }, 
] 

StreamReader.ReadToEnd()將返回我想要的一切,但它正在取代西裏爾字母數字如:\ u041c \ u0410 \ u0422 \ u0422 \ u041e

我應該怎麼做才能正確地得到一切?

回答

0

你用Unicode試過了嗎?

StreamReader readStream = new StreamReader (receiveStream, Encoding.Unicode); 

或者這樣:

System.Text.UnicodeEncoding encodingUNICODE = new System.Text.UnicodeEncoding(); 
byte[] textBytesCyrillic = encodingUNICODE.GetBytes(readStream.ToString()); 

Console.WriteLine ("Response stream received."); 
Console.WriteLine (encodingUNICODE.GetString(textBytesCyrillic)); 

在此基礎上thread你已經得到的字符串的形式西里爾值。如果您可以顯示西里爾文的原始形式,請嘗試提供答案。

+0

如果我將它更改爲Unicode的編碼,那麼一切都是問號。 – Dilshod 2015-01-21 05:29:41

+0

您的第一個示例將所有內容顯示爲問號,第二個示例顯示「System.IO.StreamReader」。你在電腦上試過這個嗎? – Dilshod 2015-01-21 05:39:11

+0

我的錯誤首先,我在我的電腦上試過這個,但只使用了一個簡單的字符串西里爾字符串,並且作品 – juniordeveloper 2015-01-21 06:18:34