1. pgharpure's Avatar
    Hello everybody,

    I am new to this forum as well as Windows Phone Development. I am currently developing an app in which I am working with a Web-Service and I need to make a POST request to a web service.
    I am trying to accomplish a user login functionality here for which,
    -> http://abc.com/login (URI)
    -> (PARAMETERS)
    apikey: 32 byte long alpha-numeric
    username: 3-15 characters
    password: 3-15 characters

    So for this I am trying to use WebClient class' UploadStringSync method in order to POST the data. My code is as follows.

    WebClient wc1 = new WebClient();
    wc1.UploadStringAsync(new Uri("http://abc.com/login"),"POST","?apikey=" + Apikey + "&username=username&password=password");
    wc1.UploadStringCompleted += new UploadStringCompletedEventHandler(wc1_UploadStringCompleted);
    //
    void wc1_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
    MessageBox.Show(e.Result);
    //
    }

    Execution stops here at MessageBox line and throws message saying 'The remote server returned an error: NotFound.'

    Is there any problem with the way I am passing the parameters? I tried to search for the working implementation everywhere but was unable to find it.

    Can anybody help me with this? This is a starting point of my project and really need help on this one. Any help would be much appreciated.

    Thank you.

    Cheers!
    02-24-2012 11:00 AM
  2. Gmotagi's Avatar
    I really need to write a post how to do this for people and put on apphub. I'm sorry I dont have time for a full explaination as im off to mwc in a couple of hours but you usually get that message when your data format isnt correct. (I'm assuming you are connecting to a rest service).

    Briefly the way I do it for json :-
    On server,
    Create a WCF REST project using the vsisual studio template, and make sure to declarate the method with [WebInvoke(UriTemplate = "/login", Method = "POST", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]

    For the client & server have a class that represents your object e.g.
    public class DataObj
    {
    public string ApiKey{ get; set; }
    public string Username{ get; set; }
    public string Password{ get; set; }
    }

    That will be passed to the server API e.g.
    void login(DataObj instance);

    Then when you call it from the client call it like this:-
    WebClient wc = new WebClient();
    if (wc != null)
    {
    Uri uri = new Uri( "http://abc.com/servicename/login/" );
    wc.UploadStringAsync( uri, "POST", data);
    }

    where data would be the JSON representation of you data, in this case :-

    {"ApiKey":"1234", "Username":"abc", "Password":"54321"}

    Hope that helps you a bit
    02-26-2012 07:33 AM
LINK TO POST COPIED TO CLIPBOARD