HttpWebRequest-Post-method

HttpWebRequest-Post-method
In this post, I will Check "Yahoo online user status" using ASp.net and HttpWebRequest POST method. You can see the below screen shot. Also watch video about HttpWebRequest in Asp.net.

Add Button control in your web page and write down below code to see yahoo user online status. Your aspx page will look like below one

 <form id="form1" runat="server">

<div>

Status:
<asp:Label
ID="Label1" runat="server" Text="Label" BackColor="#FFFF80" ForeColor
="#0000C0">
</
asp:Label><br />

Yahoo user name:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text=":) Check Online Status !" />

</div>

</form>



On click event of button, we will post the user name to Yahoo and Yahoo will response the user online status.

We will use HttpWebRequest post method and HttpWebResponse to receive the response.

Add below name space in your code  

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;


  

public partial class Default2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = "";

}

protected void Button2_Click(object sender, EventArgs e)

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://opi.yahoo.com/online");

request.Timeout = 9 * 1000; // set request timeout = 9 second. if yahoo api failed to respond in 9 second request would get timed out.

request.Method = "Post"; // we will post the data using post method

string postData = "u=" + TextBox1.Text + "&m=s&t=8" ;

// data to be posted using HttpWebrequest post method

// we will post parameter u , m and t

// Convert this string into stream of bytes

byte[] arrPostDAta = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);

// set request content length = post data length

request.ContentLength = arrPostDAta.Length;

System.IO.Stream strmPostData = request.GetRequestStream();

// get request stream

// write post data to stream of request

strmPostData.Write(arrPostDAta, 0, arrPostDAta.Length);

strmPostData.Close();

// upload post data and Get Response from server

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

Label1.Text = reader.ReadToEnd();

reader.Close();

response.Close();

}

}



HttpWebRequest and HttpWebResponse objects can be found under namepsace System.Net;

Label will show you yahoo online user status.

I learned HttpWebRequest and HttpWebResponse using this article
http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm

You can also watch below 8min video to understand how it works.


Satalaj

Currently rated 4.3 by 4 people

  • Currently 4.25/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Author

code tutorial