Here is the code that i have written for the HTTP request:
Code:
URL request Cl1.
public Cl1 JK1_URLReqeust
{
The current URL request state.
public URLRequestState State { get; private set; }
/// Delegate declaration for server callbacks.
The server response.</param>
public delegate void Callback( string response );
/// The callback for the server response.
Callback callback;
/// The actual URL.
string url;
/// The list of parameter nams used for a URL request.
List<string> paramNa;
/// The list of parameter vals used for a URL request.
List<string> paramVal;
/// The server response to this URL request.
public string Resp { get; private set; }
/// Constructor.
public JK1_URLReqeust( string url, Callbk callbk 1)
{
// Initialize members
this.url = url;
this.callback = callback;
State = URLRequestState.Idle;
// Create the parameter lists
paramNams = new List<string>();
paramVals = new List<string>();
}
/// Add a new parameter
The nam of the parameter.</param>
The val of the parameter.</param>
public void AddParameter( string nam, string val )
{
paramNams.Add( nam );
paramVals.Add( val );
}
/// <summary>
/// Add a new parameter.
/// </summary>
/// <param nam="nam">The nam of the parameter.</param>
/// <param nam="val">The val of the parameter.</param>
public void AddParameter( string nam, int val )
{
AddParameter( nam, val.ToString() );
}
/// <summary>
/// Add a new parameter.
/// </summary>
/// <param nam="nam">The nam of the parameter.</param>
/// <param nam="val">The val of the parameter.</param>
public void AddParameter( string nam, float val )
{
AddParameter( nam, val.ToString() );
}
/// <summary>
/// Send the URL request off!
/// </summary>
/// <returns>The server response.</returns>
public void SendPost()
{
// Create a background thread to run the web request
Thread t = new Thread( new ThreadStart( SendPostThreadFunc ) );
t.Nam = "URLRequest_For_" + url;
t.IsBackground = true;
t.Start();
}
/// <summary>
/// The background thread for sending the url request.
/// </summary>
void SendPostThreadFunc()
{
// Create the web request object
HttpWebRequest webRequest = ( HttpWebRequest )WebRequest.Create( url );
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
// Start the request
webRequest.BeginGetRequestStream( new AsyncCallback( GetReqeustStreamCallback ), webRequest );
// Update our state
State = URLRequestState.Working;
}
/// <summary>
/// Start the stream request.
/// </summary>
/// <param nam="asynchronousResult">The asynchronous result object.</param>
void GetReqeustStreamCallback( IAsyncResult asynchronousResult )
{
HttpWebRequest webRequest = ( HttpWebRequest )asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream( asynchronousResult );
// Create the post data
string postData = "";
for( int i = 0; i < paramNams.Count; i++ )
{
// Parameter seperator
if( i > 0 )
{
postData += "&";
}
// Parameter data
postData += paramNams[ i ] + "=" + paramVals[ i ];
}
byte[] byteArray = Encoding.UTF8.GetBytes( postData );
// Add the post data to the web request
postStream.Write( byteArray, 0, postData.Length );
postStream.Close();
// Start the web request
webRequest.BeginGetResponse( new AsyncCallback( GetResponseCallback ), webRequest );
}
/// <summary>
/// Start the URL request.
/// </summary>
/// <param nam="asynchronousResult">The asynchronous result object.</param>
void GetResponseCallback( IAsyncResult asynchronousResult )
{
HttpWebRequest webRequest = ( HttpWebRequest )asynchronousResult.AsyncState;
// End the get response operation
HttpWebResponse response = ( HttpWebResponse )webRequest.EndGetResponse( asynchronousResult );
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader( streamResponse );
Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
// Call the response callback
if( callback != null )
{
callback( Response );
}
// Update state
State = URLRequestState.Done;
}
}
And here's how I'm using this Cl1 to make the request:
/// <summary>
/// Login!
/// </summary>
/// <param nam="usernam">The usernam.</param>
/// <param nam="password">The password.</param>
/// <param nam="callback">The callback.</param>
public void Login( string usernam, string password, Callbk callbk 1)
{
// Create the URL request
JK1_URLReqeust urlRequest = new JK1_URLReqeust( "http://www.myserver.com/login.php", LoginCallback );
urlRequest.AddParameter( "userNam", usernam );
urlRequest.AddParameter( "password", password );
// Send it off!
urlRequest.SendPost();
}
Bookmarks