Let's send "POST" data from our Windows Phone and get response.
- Create Windows Phone Application named "SendPOSTdata".
- In MainPage.xaml.cs include some "using":
using System; using System.IO; using System.Text; using System.Diagnostics; using System.Net; using System.Windows; using Microsoft.Phone.Controls;
- Create loaded event handler:
using System; public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { System.Uri myUri = new System.Uri("http://myPageUrlAddress.com/"); HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),myRequest); }
- Now we need create our "POST" data stream:
void GetRequestStreamCallback(IAsyncResult callbackResult) { HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState; // End the stream request operation Stream postStream = myRequest.EndGetRequestStream(callbackResult); // Create the post data string postData = "param1=value1¶m2=value2"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Add the post data to the web request postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); // Start the web request myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest); }
- And at the end receive response:
void GetResponsetStreamCallback(IAsyncResult callbackResult) { HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult); using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream())) { string result = httpWebStreamReader.ReadToEnd(); //For debug: show results Debug.WriteLine(result); } }
- Run.
if I want to send image from image1, how I can put this in code
ReplyDeletestring postData = "param1=value1¶m2=value2";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
?
how about server side?
ReplyDeleteCan you explain ,if i want to use database which is made in my local pc which is in sql server 2008 ,then what is the way to store in it and retrive data form it.please help me
ReplyDeleteThanks in advance.
how to upload image using HttpWebRequest to server?
ReplyDeletehey, i have this web page that has a form that can take in a user name and a password, i want my wp app to take these entries from user, and then submit the form, the background web page must not be visible to the user, is there a way to do so?
ReplyDeleteAm getting error
ReplyDelete+ $exception {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.b__d(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.b__0(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at Line_App.MainPage.GetResponsetStreamCallback(IAsyncResult callbackResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass1d.b__1b(Object state2)} System.Exception {System.Net.WebException}
That particular error can mean anything, it is just the generic error that WCF uses to say "something went wrong". Read about it
Deletehttp://social.msdn.microsoft.com/Forums/en-US/18898e77-6b61-4b67-9187-f754c7bfaabc/systemnetwebexception-the-remote-server-returned-an-error-notfound?forum=synclab
http://stackoverflow.com/questions/2920916/the-remote-server-returned-an-error-notfound
http://stackoverflow.com/questions/21176574/system-net-webexception-the-remote-server-returned-an-error-notfound-with-post
System.Net.WebException was unhandled by user code
ReplyDeleteHResult=-2146233079
Message=The remote server returned an error: NotFound.
Source=System.Windows
This Error i got Please Correct me .....
Thank you! This helped me today.
ReplyDelete{System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
ReplyDeleteat System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.
This comment has been removed by the author.
ReplyDelete