Post with WebRequest

13,545

Solution 1

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.

Solution 2

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.

Solution 3

Not sure what you've already used, but have you tried WebClient?

WebClient web = new WebClient();
web.DownloadStringCompleted += (s, e) =>
{
    if (e.Error == null)
        CodeHereToHandleSuccess();
    else
        CodeHereToHandleError(e.Error);
};
web.DownloadStringAsync(new Uri(theURLYoureTryingToUse));

There's also WebRequest to look at too, that might work for what you're doing.

Edit: As for your "POST" edit, webclient lets you do post:

        web.OpenWriteAsync(new Uri(theURLYoureTryingToUse), "POST");

you also then have to add an OpenWriteCompleted handler.

not sure exactly what you're doing, so you'll need to add more info to your question.

Share:
13,545
instigator
Author by

instigator

Updated on June 04, 2022

Comments

  • instigator
    instigator almost 2 years

    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.