Question Accessing webpage

mikrophun

Member
Joined
Jan 22, 2013
Messages
7
Programming Experience
Beginner
C#:
private void btnGetResponse_Click(object sender, EventArgs e)        {
            //string responseFromServer;


            WebClient c = new WebClient();
            string url = tbLink.Text;
            var data = c.DownloadString(url);


            rtbGetResponse.Text = data;
        }
C#:
private void btnGetResponse_Click(object sender, EventArgs e)        {
            string url = tbLink.Text;
            
            // Create a request for the URL.         
            WebRequest request = WebRequest.Create(url);
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            
            
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content. 
            string responseFromServer = reader.ReadToEnd();
            
            // Display the content.
            rtbGetResponse.Text = responseFromServer;
            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
Dear all
above snippet code have similar function, and also get similiar response.
things that make me wondering is, whats the difference between both of it?
and which is the best to use?

pls give me some opinion
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,144
Location
Sydney, Australia
Programming Experience
10+
You should use a WebClient if you can because it is simpler. If the simple interface provided by the WebClient class cannot do what you need then you would use a WebRequest. The WebClient actually uses a WebRequest internally.
 

mikrophun

Member
Joined
Jan 22, 2013
Messages
7
Programming Experience
Beginner
Above code is using Get method. if we want to use post method we can use the code like this:

C#:
private void btnGetResponse_Click(object sender, EventArgs e) {
string url = tbLink.Text;
string name = tbName.Text;
string age = tbAge.Text;


string myParameters = "name=" + name + "&age=" + age;
//change 'space' character into '%20' character
myParameters = myParameters.Replace(" ", "%20");


WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(url, myParameters);


rtbGetResponse.Text = HtmlResult;
}

and the next code is when we are using WebRequest:
C#:
private void btnGetResponse_Click(object sender, EventArgs e)        {
            string url = tbLink.Text;
            string name = tbName.Text;
            string age = tbAge.Text;


            string myParameters = "name=" + name + "&age=" + age;
            //change 'space' character into '%20' character
            myParameters = myParameters.Replace(" ", "%20");


            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(url);
            // Set the Method property of the request to POST.
            request.Method = "POST";


            byte[] byteArray = Encoding.UTF8.GetBytes(myParameters);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();


            // Display the content.
            rtbGetResponse.Text = responseFromServer;


            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
 
Last edited:
Top Bottom