I have a site whose content is in French language.
Now I want to get these through HttpWebRequest
and HttpWebResponse
in console application using c#.
public string GetContents(string url)
{
StreamReader _Answer;
try
{
开发者_C百科 HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "utf-8");
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1;)";
WebReq.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
_Answer = new StreamReader(Answer, Encoding.UTF8);
return _Answer.ReadToEnd();
}
catch
{
}
return "";
}
I get the content but it contain some strange symbol like squares etc.
Are you sure the web server is responding with UTF-8 encoding?
Update:
The web server from which you are trying to download is serving the pages with a character encoding of ISO-8859-1
and not UTF-8
.
You have to (a) change your hard coded content type or (b) read the content type from the server response and use that.
精彩评论