I have an URL and i want only to check the response code of the page and not the complete page source as fetching the the complete page source is quite slow. what is right way to go ?
does getResponseCode() in HttpUrlConnection feches the complete page source or only the header ?
Straight from the docs, HttpUrlConnection#getResponseCode()
Gets the status code from an HTTP response message. For example, in the case of the following status lines:
HTTP/1.0 200 OK
HTTP/1.0 401 Unauthorized
It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).
Depends what you're motivation is in making the request. If the request is normally just a GET request for a resource and doesn't have any side effects.
You can perform a HTTP HEAD request instead, which if implemented correctly should get you the same status codes but not the body. (i.e. setRequestMethod(HEAD))
Here method may be PostMethod or GetMethod
you can get status code from an HTTP response message by getStatusCode()
example:-
int statuscode=method.getStatusCode();
Related
My API is calling some other service with URL
https://idgenvip.qa.ch3.s.com/IDGen/services/id/generate?idType=GIFT_REGISTRY_ID
and this service is returning me HTTP response code 406
which is not acceptable.
UtilHelper:691 - Exception occurred while generating id : Server
returned HTTP response code: 406 for URL:
https://idgenvip.qa.ch3.s.com/IDGen/services/id/generate?idType=GIFT_REGISTRY_ID
I'm not passing accept header in my request. What could be the other reason I'm getting this?
To be frank with you, a 406 response could be returned for any reason ... or no reason at all.
According to the HTTP 1.1 specification (Section 6.5.6) it should be returned is the one of these headers is requesting output in a representation that the server cannot provide:
Accept
Accept-Charset
Accept-Encoding
Accept-Language
If the server implementation is following the spec, the response body should list the representations that it can provide. (Look at the response body to see if it is saying anything.)
What else could it be?
It could conceivably be that the server is telling you that you need to provide an "accept" header.
It could conceivably be that the server is giving a deliberately misleading response, because it thinks you are abusing the API.
But I'm really guessing. As I said at the beginning ... it could be anything.
My advice would be to read the API documentation carefully, and ask the people who run the server what you are doing wring.
In my case when I checked the code, The accept header was hard coded in the code itself.
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
connection.setRequestProperty("Content-Type", "application/json");
So when I removed/commented out the code the service stopped throwing 406. :-)
I am giving request to OData POST in Json format and it's returning the same request to me.
POST URL= http://localhost:8085/MagicXpiOData/Odata_get.OData_1/Student_details
Body:
{"Division": "Nashik"}
Content-Type = application/json
Response:
{
"#odata.context": "$metadata#Student_details",
"Division": "Nashik"
}
Is this a correct response?
I think it should return a status code for success or not.
What you show above is the response body, which usually contains the OData.Context. Beside that you should find the HTTP Status Code in the header, which might be something like
HTTP/1.1 200 OK
HTTP/1.1 201 Created
and when you create a new entry also a location header.
Some examples that show typical requests and responses can be found in this Basic Tutorial
So generally your response looks ok. Maybe you can add the response header in your question, then we can see if everything is there what should be there.
When you send your request with a tool like Postman or Fiddler, you might by default only see the response body. To see the headers you need to switch to Headers, or Raw in Fiddler to see the full response (header and body).
Can we add message body for the error on HTTP 401 response. On 500 sereies I was getting the error response body but on 401 I am not been able to get the body though the body is set in the server?
Please suggest.
An HTTP 401 means Unauthorized. From RFC 2616, it can be read:
The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource.
The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials.
If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].
While this is a bit generic, it showns no restriction on what the body of the request can/cannot contain. Check on your tool on what/how to reply for non HTTP 200 (OK).
In a java web-app I write to my HttpServletResponse:
httpResponse.getWriter().write(someJsonString);
httpResponse.getWriter().flush();
The client (apache jmeter in this case) gets the response with the json in the body and status 200 as expected.
If I decide to change the response status:
httpResponse.getWriter().write(someJsonString);
httpResponse.setStatus(Response.Status.NO_CONTENT.getStatusCode());
httpResponse.getWriter().flush();
My client gets the response with the right status (204 in this case) but an empty body for some reason.
What can cause this?
When you send response as 204, it means there is no body.
See w3c rfc
The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.
It means while sending response either container is not considering body or your client is discarding after reading status in response.
One way could be to check this response in web-browser if possible. With tools like fire-bug or similar in Chrome you could actual check response.
According to W3 Meta refresh is discouraged and they recommend Server side redirection. So my question is how am I supposed to do that in HTTPServer?
The API does not provide any redirection method here:
http://docs.oracle.com/javase/7/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html
If this "redirection" is not that abstract thing, then what would be the idea behind that?
What the W3 means when is says this, is that instead of your HTTP server returning a status-code of 200 (OK) with a response body that has HTML with a
<META HTTP-EQUIV=REFRESH CONTENT="1; URL=http://www.example.org/bar">
you have your HTTP server return a suitable 3xx (redirect) status code, with a Location header that gives the location to be redirected to.
The most suitable status code would be 302 (Found) if you still expect people to use the original URL as an alias for the URL being redirected too (the HTTP standard says "the client SHOULD continue to use the Request-URI for future requests").
If you want users to stop using the original URL the most suitable status code would be 301 (Moved Permanently).