2015-01-21 65 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