0
I need to develop a Webapi that you pass a Query and it returns the google search with title and url. I am using C# and . NET, when I do the HTTP request to receive page data, it is returning the special characters as a "?" Someone has an idea how to fix this?
Another question is why when I make a request (get) by Postman, it returns A lot of data, and with C# (Webrequest) much less?
Follow the code:
string sURL = "https://www.google.com/search?q="+this.Query+"&sourceid=chrome&ie=UTF-8";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sURL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string myResponse = "";
using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
{
myResponse = sr.ReadToEnd();
}
return myResponse;
I did some tests with other websites, and they perfectly return the characters, apparently the problem is with Google (However by Postman returns perfectly)
Where is your code?
– Leandro Angelo
I just edited the question
– Felipe Chiarotti
Try passing the UTF-8 encoding as the second parameter of the Streamreader, I think it solves the character problem.
– Francisco
@Francis unfortunately did not resolve.
– Felipe Chiarotti
This code you passed works well, congratulations. But still missing the code that sends the result you found for the page because that’s where the problem is the way the HTML is being passed, the image is being passed with a text .
– Augusto Vasques
@Augustovasques this, I’m passing as text, then filter this text to get only the titles and urls (the part that interests me). However, what is happening, are these invalid characters that apparently come directly from the request, because if I make requests to sites like globe.com and Uol.com.br, the characters arrive perfect.
– Felipe Chiarotti
Uf8 and Htmlencode / Htmldecode
– Leandro Angelo
Unfortunately it didn’t work either.
– Felipe Chiarotti
It is not the case to make then one
String.Replace(char, char)
?return myResponse = myResponse.Replace('\ufffd',' ')
– Augusto Vasques
The problem is that all characters in ufffd are characters with some type of accent, whatever ~ ` ^
– Felipe Chiarotti
@Felipechiarotti, view my answer?
– Leandro Angelo