androidcontent-typeandroid-networking

How to set DefaultHttpClient to accept a special content type


I use a DefaultHttpClient to scan the LAN to find addresses. Some computer provide OData (WCF) services. The url looks like this:

http://someurl/Service/Service.svc/

If I open it in a browser, it shows XML.

If I try to connect to an address where there is no computer, I get standard 404 as status code. If I find a computer available, I get a 405 code, which is a Method Not Allowed, and according to this I should set something:

The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

I'd like to get status code of 200 instead of 405. What should I set for the DefaultHttpClient to accept the xml as content?

Here is the code I use (based on this answer by kuester2000):

DefaultHttpClient hc = new DefaultHttpClient();
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 700;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 700;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
hc.setParams(httpParameters);
HttpPost hp = new HttpPost(url);
HttpResponse hr= hc.execute(hp);
if (hr.getStatusLine().getStatusCode() == 405) {
    // Do something...
}

Solution

  • The URL was the problem. It looked like this:

    http://someurl/Service/Service.svc/
    

    When I modified to this:

    http://someurl/Service/Service.svc
    

    I get my status code as 200.