How can I get a response from a server in WP7? - windows-phone-7

I'm trying to send a request with the HttpWebRequest class on WP7, but I don't get any response...
Here is my code:
InitializeComponent();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
tbResponse.Text = reader.ReadToEnd();
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();
Moreover, I use this extension: click here, but I tested it on a Windows Console Application and there wasn't any problem, so I think the problem is that I don't know something about WP7.

You need to make asynchronous requests like this:
var webRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
webRequest.BeginGetResponse(new AsyncCallback(request_CallBack), webRequest );
and the response handler:
void request_CallBack(IAsyncResult result)
{
var webRequest = result.AsyncState as HttpWebRequest;
var response = (HttpWebResponse)WebRequest.EndGetResponse(result);
var baseStream = response.GetResponseStream();
// if you want to read binary response
using (var reader = new BinaryReader(baseStream))
{
DataBytes = reader.ReadBytes((int)baseStream.Length);
}
// if you want to read string response
using (var reader = new StreamReader(baseStream))
{
Result = reader.ReadToEnd();
}
}
Here is a helper class I developed to handle my web request needs during development of windows phone 7 apps:
http://www.manorey.net/mohblog/?p=17&preview=true

Related

Loading issue in WebBrowser control for httpwebresponse in WP7 & 8

I am trying to load web browser with the html code from the httpwebresponse by storing it in a string variable.
I created a httpwebrequest for a URL by setting cookies in it and getting a html page in a httpwebResponse.
I am trying to display it in a web browser control by saving the code in a string variable. And navigating the browser("browse.NavigateToString(string)").
But here page is not loaded properly.It is not displaying any css or images and displaying a message"You need to turn on java script in order to display the contents".
I made the web browser property (IsScriptEnabled ) true before navigation. But it is still showing the message and contents are not loaded properly.
Cookies are also not injected in to the web browser controls(session is not maintained through out the app).
Is there any solution or work around for this ??
Please refer to the below code :
To log cookies from login service :
private void PostLoginRequest()
{
string AuthServiceUri = "Service URL";
HttpWebRequest AuthReq = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;
AuthReq.CookieContainer = new CookieContainer();
AuthReq.ContentType = "application/x-www-form-urlencoded";
AuthReq.Method = "POST";
AuthReq.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), AuthReq);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest Request = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = Request.EndGetRequestStream(callbackResult);
string postData = "Postdata parameters";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), Request);
}
void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
try
{
HttpWebRequest callBackRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
CookieCollection cookiecollec = new CookieCollection();
cookiecollec = response.Cookies;
App.cookieCollection = cookiecollec; //global varaiable
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Another httpwebrequest with logged cookies :
private void ResponsivePostURL(string PageName, string PostURL, string PostDatas)
{
Uri ServiceUri = new Uri(PostURL); //Service URL
HttpWebRequest requestURL = HttpWebRequest.CreateServiceUri as HttpWebRequest;
requestURL.ContentType = "application/x-www-form-urlencoded";
requestURL.Method = "POST";
requestURL.CookieContainer = new CookieContainer();
requestURL.CookieContainer.Add(ServiceUri, App.cookieCollection);
requestURL.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), requestURL);
}
void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
// MessageBox.Show(response.Cookies.ToString());
string responseString = "<html><head></head><body>";
Stream streamResponse = response.GetResponseStream();
StreamReader reader = new StreamReader(streamResponse);
responseString = responseString + reader.ReadToEnd() + "</body></html>";
streamResponse.Close();
reader.Close();
response.Close();
this.Dispatcher.BeginInvoke(() =>
{
webBrowser.IsScriptEnabled = true;
webBrowser.NavigateToString(responseString);
});
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
When you navigate to the site and save the document, you are not getting the script, css nor image files (and any other resource for that matter) but only the static/generated HTML document as-is, which probably refer to those files using relative paths. If paths are not absolute, scripts/css/images/other resources are not found.
If you want to make it work, you need to replace relative paths with absolute paths within the HTML file.
So if you have
<script src="/scripts/cool.js"></script>
You'd replace it with
<script src="http://www.thesite.com/scripts/cool.js"></script>
(Note that this is just my best guess, not something I have actually tried out :)

HttpWebRequest Failing, Can't Figure out Why

I have a WP7 app where I'm trying to reconstruct an HTTPWebRequest that I have successfully written elsewhere using the synchronous methods (pasted at end) but which doesn't work in WP7, I assume because I'm doing something wrong with the Asynchronous versions of these methods.
I believe the issue stems from the fact that the non-working code on the Compact Framework can only send a bytearray[] - I don't have the option of sending the json string. If I send a bytearray in the code that works, I get an error there too. Is there a different option?
Here is my code - this does not work. The exception is thrown on the 2nd line of the last method - "Using(var respons ...)":
public void CreateUser()
{
var request = (HttpWebRequest)WebRequest.Create("http://staging.cloudapp.net:8080/api/users/");
request.Method = "POST";
request.ContentType = "text/json; charset=utf-8";
request.BeginGetRequestStream(new AsyncCallback(RequestCallback), request);
}
private static void RequestCallback(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
using (Stream postStream = request.EndGetRequestStream(result))
{
User user = new User("Windows", "Phone", "USCA");
Formatting formatting = new Formatting();
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
string json = JsonConvert.SerializeObject(user, formatting, settings);
byte[] byteArray = Encoding.UTF8.GetBytes(json);
postStream.Write(byteArray, 0, json.Length);
}
request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
}
private static void ResponseCallback(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
using (var response = (HttpWebResponse)request.EndGetResponse(result))
{
using (Stream streamResponse = response.GetResponseStream())
{
StreamReader reader = new StreamReader(streamResponse);
string responseString = reader.ReadToEnd();
reader.Close();
}
}
}
This code works (non-compact framework version of the same request):
HttpWebRequest request = HttpWebRequest.Create("http://staging.cloudapp.net/api/users/") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text/json";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
User user = new user("Other", "Guy", "USWC");
Formatting formatting = new Formatting();
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
string json = JsonConvert.SerializeObject(user, formatting, settings);
writer.Write(json);
}
var response = request.GetResponse() as HttpWebResponse;
using (var reader = new StreamReader(response.GetResponseStream()))
{
var responseText = reader.ReadToEnd();
return responseText;
}
thanks for any help!
looks like the server is responding with a "404 not found". Does the resource you are requesting exist at the server?
Does your JSON contain any non 7-bit ASCII characters, as you are currently doing:
byte[] byteArray = Encoding.UTF8.GetBytes(json);
postStream.Write(byteArray, 0, json.Length);
The number of bytes might not be identical to the number of characters in your string, which could lead to a malformed request.
It would be worthwhile using something like Fiddler to verify what is actually going over the wire from the emulator or phone (there are instructions on the Fiddler website for how to do this)
Well - I'm not sure why this problem went away. I liked #RowlandShaw's suggestion, but I didn't actually change anything in the json. Wish I could give a better solution.

How to send data using webrequest class method?

In my application I send data to a PHP server. I got code from the msdn website.
void SendPost()
{
var url = "http://www.nuatransmedia.com/ncampaign/mailserver/mail1.php";
// 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(GetRequestStreamCallback), webRequest);
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
// Demo POST data
string postData = "Hello";
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
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
var Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
// Error treatment
// ...
}
}` void SendPost()
{
var url = "http://www.nuatransmedia.com/ncampaign/mailserver/mail1.php";
// 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(GetRequestStreamCallback), webRequest);
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
// Demo POST data
string postData = "Hello";
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
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
var Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
// Error treatment
// ...
}
}
I got mail. But the body messages not show in my mail. Where did I make a mistake? In my PHP code I use postData variable to get my message.
For simpler HTTP usage the classes at http://mytoolkit.codeplex.com/wikipage?title=Http might help you...
These classes help to HTTP POST files (access via $_FILES["..."]). Also there is a RawData property (byte[]) to set the data directly.
(also GZIP, own timeout, and more is supported)

how to make HTTP POST using reactive extension on windows phone 7

I found an example about HTTP POST in msdn, but I am wondering how can I make use of reactive extensions here.
using System;
using System.Net;
using System.IO;
using System.Text; using System.Threading;
class HttpWebRequestBeginGetRequest
{
private static ManualResetEvent allDone = new ManualResetEvent(false);
public static void Main(string[] args)
{
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.contoso.com/example.aspx");
request.ContentType = "application/x-www-form-urlencoded";
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
// start the asynchronous operation
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
// Keep the main thread from continuing while the asynchronous
// operation completes. A real world application
// could do something useful such as updating its user interface.
allDone.WaitOne();
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
Console.WriteLine("Please enter the input data to be posted:");
string postData = Console.ReadLine();
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, postData.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
allDone.Set();
}
}
I am trying to use the following code, but it does not work. Can anyone help me out on this?
Thanks in advance -Peng
return (from request in
Observable.Return((HttpWebRequest)WebRequest.Create(new Uri(postUrl))).Catch(Observable.Empty<HttpWebRequest>())
.Do(req =>
{
// Set up the request properties
req.Method = "POST";
req.ContentType = contentType;
req.UserAgent = userAgent;
req.CookieContainer = new CookieContainer();
Observable.FromAsyncPattern<Stream>(req.BeginGetRequestStream, req.EndGetRequestStream)()
.ObserveOnDispatcher()
.Subscribe(stream =>
{
stream.Write(formData, 0,
formData.Length);
stream.Close();
})
;
})
from response in
Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse)().Catch(Observable.Empty<WebResponse>())
from item in GetPostResponse(response.GetResponseStream()).ToObservable().Catch(Observable.Empty<string>())
select item).ObserveOnDispatcher();
Edit: To make it clear, I want to use the rx to implement the same logic in MSDN example.
in the MSDN example, it seems it first makes async call to write RequestStream, and then in the GetRequestStreamCallback, fires another async call to get the response.
Using Rx, I am able to create 2 observables
1. Observable.FromAsyncPattern(request.BeginGetRequestStream, request.EndGetRequestStream)()
2. Observable.FromAsyncPattern(request.BeginGetResponse, request.EndGetResponse)()
The problem is the second observable depends on the first one's result, so how can I do this in Rx?
In the first observable's subcribe method to create the seond observable? is it the good way?
This is how I am doing it. I configure the two Async patters up front, then use SelectMany to chain them together.
I have cut out the error handling etc from this code to keep it simple and show only the bare minimum to get it working. You should append a .Catch() similar to your own code, and if you want to get more than just a string out (say the response code) then you'll need to create a class/struct to hold all the bits of data you need and return that instead.
public IObservable<string> BeginPost(Uri uri, string postData) {
var request = HttpWebRequest.CreateHttp(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var fetchRequestStream = Observable.FromAsyncPattern<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream);
var fetchResponse = Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse);
return fetchRequestStream().SelectMany(stream => {
using (var writer = new StreamWriter(stream)) writer.Write(postData);
return fetchResponse();
}).Select(result => {
var response = (HttpWebResponse)result;
string s = "";
if (response.StatusCode == HttpStatusCode.OK) {
using (var reader = new StreamReader(response.GetResponseStream())) s = reader.ReadToEnd();
}
return s;
});
}
Your problem is your use of Do() here, you need to move the GetRequestStream into your SelectMany (into your "from bla in, from bla in"...), since it only makes sense to get the response stream after you've written the full request. Right now, you're trying to do both concurrently.

unable to add cookie to httpwebrequest recieved from the first request in wp7

cookie recieved from first request:
private void PostResponseCallback(IAsyncResult asyncResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
Stream content = response.GetResponseStream();
SESSIONID = response.Headers["Set-cookie"].ToString();
if (request != null && response != null)
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(content))
{
string _responseString = reader.ReadToEnd();
ResponseString = _responseString;
reader.Close();
}
}
}
}
Unable to set cookie in second request
public void AccDetialsGetResponse()
{
try
{
//CookieContainer cc1 = new CookieContainer();
CookieCollection collection = new CookieCollection();
SESSIONID = SESSIONID.Replace("; Path=/", "");
Cookie cook = new Cookie();
cook.Name = "cookie";
cook.Value = SESSIONID;
collection.Add(cook);
//cc1.Add(new Uri(strHttpsUrl), cccs);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strHttpsUrl);
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(new Uri(strHttpsUrl), collection);
req.BeginGetRequestStream(AccDetailsPostRequest, req);
}
Kindly provide a solution to the above issue...
It turns out you are better off capturing the cookie store from the first request (like so)
//keep in mind ResponseAndCookies is just a hand rolled obj I used to hold both cookies and the html returned during the response
private ResponseAndCookies ReadResponse(IAsyncResult result)
{
Stream dataStream = null;
HttpWebRequest request = (HttpWebRequest) result.AsyncState;
HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
var responseAndCookies = new ResponseAndCookies
{CookieContainer = request.CookieContainer, Markup = responseFromServer};
return responseAndCookies;
}
And using this cookie store directly when you create then new request. (instead of manually adding the cookie like you had initially)
public void Checkout(ResponseAndCookies responseAndCookies)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/checkout");
request.ContentType = "application/json";
request.CookieContainer = responseAndCookies.CookieContainer;
request.Method = "POST";
request.AllowAutoRedirect = false;
request.Accept = "application/json";
request.BeginGetRequestStream(new AsyncCallback(GetRequest), request);
}
Note- if the cookies you are passing around are HTTP ONLY this is actually the ONLY way to deal with them (in the current release of windows phone 7)

Resources