Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post with WebRequest

I am trying to post to google so I can log into Google Reader and download subscription list, but I am unable to find a way to post to google in the windows 7 phone sdk, does anyone have an example on how to do this?

*Edit: Sorry wasn't very clear I am trying use the POST method, to submit an email and password to google login and retrieve a sid. I have used WebClient and HttpWebRequest but all the examples I have seen to send post data, the api calls are not in the windows 7 phone sdk.

like image 549
instigator Avatar asked Jul 02 '26 14:07

instigator


2 Answers

I don't know anything about the Google API you're trying to use, but if all you need is to send a POST request, you can certainly do that with WebClient or HttpWebRequest. With WebClient, you can use either WebClient.OpenWriteAsync() or WebClient.UploadStringAsync(), the documentation is here: http://msdn.microsoft.com/en-us/library/tt0f69eh%28v=VS.95%29.aspx

With HttpWebRequest, you'll need to set the Method property to "POST". Here's a basic example:

var request = WebRequest.Create(new Uri(/* your_google_url */)) as HttpWebRequest;
request.Method = "POST";
request.BeginGetRequestStream(ar =>
{
    var requestStream = request.EndGetRequestStream(ar);
    using (var sw = new StreamWriter(requestStream))
    {
        // Write the body of your request here
    }

    request.BeginGetResponse(a =>
    {
        var response = request.EndGetResponse(a);
        var responseStream = response.GetResponseStream();
        using (var sr = new StreamReader(responseStream))
        {
            // Parse the response message here
        }

    }, null);

}, null);

The WebClient class may be easier to use, but is less customizable. For instance, I haven't seen a way to be able to attach cookies to WebClient requests, or a way to set the Content-Type header when using WebClient.

like image 192
elcelista Avatar answered Jul 05 '26 04:07

elcelista


Have you tried using RESTSharp for your Windows Phone 7 project? The latest release supports Windows Phone 7 and I have had no issues working with popular REST APIs with it. In your particular case where you are trying to use the Google Reader API, this article by Luke Lowry can possibly help.

like image 22
indyfromoz Avatar answered Jul 05 '26 02:07

indyfromoz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!