Could not post from "Android REST Client" to "Visual Studio 2010 REST Server"

Jul 6, 2015 at 3:44 PM
Edited Jul 6, 2015 at 4:33 PM
Hi,

I have developed a REST client in Android Studio (Android Kitkat 4.4.2) using HttpUrlConnection class. REST Server is implemented in Visual Studio 2010 using casablanca REST sdk. (Because my company is still using VS 2010 so I could not do it on VS 2013)

Since this project is initial phase, I just wrote simple code so that I could check if communication between Server-Client can be established or not.

Visual Studio -->
int main(int argc, wchar_t* argv[])
{
http_listener listener(L"http://localhost:8080/demo");
    
listener.support(methods::GET, [] (http_request request) 
{
    request.reply(status_codes::OK, U("In Get Request"));

});

listener.support(methods::POST, [] (http_request request) 
{
    wcout << "In Post";

});

        
    try
    {
         listener
             .open()
             .then([&listener](){wcout << "starting to listen";})
             .wait();
                
         while (true);
     }
    
    catch (exception const & e)
    {
         wcout << e.what() << endl;
    }

    listener.close();
}
Android:
url = new URL("http://localhost.fiddler:8080/demo");

urlConnection = (HttpURLConnection) url.openConnection();   
               
urlConnection.setDoOutput(true); //for POST
urlConnection.setRequestMethod("POST");
OutputStream out = urlConnection.getOutputStream();
writer = new OutputStreamWriter(out);
                        
try {
    writer.write("Hello there");
} catch (IOException e) {
    UpdateUi(e.getMessage());
}
GET method works absolutely fine (code is not pasted here). But Server does nothing for Post request. I do not know if problem is at Android side or at Visual Studio side.

Any suggestions or changes in code you would like to suggest?

Thanks in advance.

Abhijeet
Coordinator
Jul 6, 2015 at 10:45 PM
Hi Abhijeet,

When you say the server doesn't nothing what ends up happening on the client side for the POST request? You must be getting back an exception with error information. Also quickly glancing at the code you appear to be sending the request on the client side to localhost.fiddler, however the server is listening on localhost.

Steve
Jul 7, 2015 at 8:39 AM
Hi Steve,

On client side there is no exception thrown. This is also surprising for me.

localhost.fiddler(reverse proxy) is just a way to reach localhost. Without fiddler localhost does not give access to Android and throws exception. localhost.fiddler is working fine as I said earlier because I am able to get GET response.

I tested POST using fidller itself and it worked. I think there is no problem on server side. That means problem is on Android side which I have to resolve myself now.
I installed one more Google App "Android Rest Client" and sent POST request and server sent back the response.

Since there is no problem on C++ httplistener, I think this thread can be closed. If you still have some suggestions, I would like to hear.

Thanks.

Abhijeet