nosthertus nosthertus - 1 year ago 211
C# Question

C# how to properly make a http web GET request

i am still new on c# and i'm trying to create an application for this page that will tell me when i get a notification (answered, commented, etc..). But for now i'm just trying to make a simple call to the api which will get the user's data.

i'm using Visual studio express 2012 to build the C# application, where (for now) you enter your user id, so the application will make the request with the user id and show the stats of this user id.

here is the code where i'm trying to make the request:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Request library
using System.Net;
using System.IO;

namespace TestApplication
class Connect
public string id;
public string type;

protected string api = "";
protected string options = "?order=desc&sort=name&site=stackoverflow";

public string request()
string totalUrl = this.join(id);

return this.HttpGet(totalUrl);

protected string join(string s)
return api + type + "/" + s + options;

protected string get(string url)
string rt;

WebRequest request = WebRequest.Create(url);

WebResponse response = request.GetResponse();

Stream dataStream = response.GetResponseStream();

StreamReader reader = new StreamReader(dataStream);

rt = reader.ReadToEnd();



return rt;

catch(Exception ex)
return "Error: " + ex.Message;
public string HttpGet(string URI)
WebClient client = new WebClient();

// Add a user agent header in case the
// requested URI contains a query.

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stream data = client.OpenRead(URI);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();

return s;

the class is an object and its being accessed from the form by just parsing it the user id and make the request.

i have tried many of the examples i have looked on google, but not clue why i am getting on all ways this message "�".

i am new in this kind of algorithm, if anyone can share a book or tutorial that shows how to do this kind of stuff (explaining each step), i would appreciate it

Answer Source

Servers sometimes compress their responses to save on bandwidth, when this happens, you need to decompress the response before attempting to read it. Fortunately, the .NET framework can do this automatically, however, we have to turn the setting on.

Here's an example of how you could achieve that.

string html = string.Empty;
string url = @"";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.AutomaticDecompression = DecompressionMethods.GZip;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
    html = reader.ReadToEnd();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download