Windows phone 7 breaks the HTTP request
Hi i am using the windows phone 7 of HTC and using it form the last few months. The thing here is that i have created an application using the emulator and it is working fine in my computer but giving the errors in my windows phone 7. Whenever i try to send the HTTP request through tat application on my computer then the request is send successfully but the same request if i am trying on my phone then it is giving me the error saying that "Page not found". I am not able to understand what the error is all about and also not able to solve the problem. Please help me in this case and also let me how to deal wit this error.
Re: Windows phone 7 breaks the HTTP request
I think that there is some thing wrong with the code that you have used for the HTTP request. I am not saying that the whole code is wrong, but it may happen that the code may differ from the computer to the phone. There may be the small changer in the code that you have to make before running the same application on the same system. It will be more comfortable for me to give the solution if you will show the entire code details. So please send me that code so that i can analyze that code and study on it. And then i can give you the perfect solution for the same.
Re: Windows phone 7 breaks the HTTP request
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();
}
Re: Windows phone 7 breaks the HTTP request
I have gone through your code and it seems to me that there is a bug in it and to overcome that bug you have to do the debug test and to perform that test you have to run the following code:
Code:
public void Dtest()
{
HTTPreq hreq1 = ( HTTPreq )HTTPreq.Create( "http://google.com " );
NetwrkCred cred1 = new NetwrkCred( "myusername", "mypassword" );
hreq1.Credentials = cred1;
hreq1.Method = "GET";
IAsyncResult token = hreq1.BeginGetResponse( new AsyncCallback( GBcallback ), hreq1 );
}
public void GBcallback(IAsyncResult result)
{
WebResponse response = ( ( HTTPreq )result.AsyncState).EndGetResponse( result );
StreamReader reader = new StreamReader( response.GetResponseStream() );
string responseString = reader.ReadToEnd();
reader.Close();
}