HttpClient- Read all resources headers for a url - java

I need to check ETag from the header response of requesting URL.
I know that when request URL, then response will be html, css, js and images.
My issue: How to read header for all these resources including the html when requesting URL?
For HttpClient if you enter URL then it will give header and response only for html!! it didn't return the header for all loading resources which are involved with this URL and will be loaded associated with this URL.
if I request only css, then it will give me a right header response for this style.
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("http://www.mkyong.com/wp-content/themes/mkyong/css/prism.css");
HttpResponse response = client.execute(request);
for (Header header : response.getHeaders("ETag"))
System.out.println(header.getName()+" == "+header.getValue());
but if I tried to but the main URL:http://www.mkyong.com, then it will give me only response for the main html
How to fetch them?

Related

Apache HttpClient gzip response doesn't have Content-Encoding

When using apache-httpclient-4.x's HttpClient class to process a URL that returns gzip compressed response, there's no "Content-Encoding" header in the response we get. However, if you visit the URL directly in the browser you can get the header.
It seems there's some step that deletes some headers while processing the compression.
https://github.com/apache/httpcomponents-client/blob/73e72f226845c790e4a6e6dccaed50ee32791f45/httpclient/src/main/java/org/apache/http/client/protocol/ResponseContentEncoding.java#L124
if (decoderFactory != null) {
response.setEntity(new DecompressingEntity(response.getEntity(), decoderFactory));
response.removeHeaders("Content-Length");
response.removeHeaders("Content-Encoding");
response.removeHeaders("Content-MD5");
}
How can we keep the headers?

Grant_type missing in request

I'm trying to get token access (protocol oauth2) to dynamics 365.
That's the code that build and execute the http post request:
URI uri = new URIBuilder()
.setScheme("https")
.setHost("login.microsoftonline.com")
.setPath("/"+PropertyUtils.getInstance().getProperty("AD_TENANT_ID")+"/oauth2/token")
.setParameter("grant_type", "client_credentials")
.setParameter("client_id", PropertyUtils.getInstance().getProperty("CLIENT_ID"))
.setParameter("resource", PropertyUtils.getInstance().getProperty("RESOURCE"))
.setParameter("client_secret", PropertyUtils.getInstance().getProperty("CLIENT_SECRET"))
.build();
HttpPost post = new HttpPost(uri);
HttpResponse response = client.execute(post);
the response json is:
{"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r ...
why response tell me that grant_type is missing when it's in the request as a parameter?
You are trying to perform the request putting those parameters in the URI as query parameters. Although those parameters needs to be put in the body of the request as form url encoded.
{"Content-Type": "application/x-www-form-urlencoded"}

Handling HTTP request redirect in java

I'm writing a network android application that uses http requests to get data. The data is HTML format. I use Apache HttpClient and JSoup.
When I'm out of traffic with my mobile internet provider, I am always redirected to the providers' page saying that I should pay some money. Of course, it is a bad idea to parse this page.
How to detect occured page substitution?
This code will help you to know with is the final target of your request, if isn't the page that you asked for, is the provider page.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://www.google.com/");
HttpResponse response = httpclient.execute(httpget, localContext);
HttpHost target = (HttpHost) localContext.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);// this is the final page of the request
System.out.println("Final target: " + target);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
Thanks
If your provider is lying to you by immediately returning a 200 OK but not giving you the resource you've requested, your best option is probably to set a custom HTTP response header that your client can check before continuing.

Multipart JSON POST request

please read my post:
I need to post the image to the JSON WS with this parameters:
Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: {number_of_bytes_in_entire_request_body} -- Check your rest client API's. Some would automatically determine content-length at runtime. Eg. jersey client api.
--foo_bar_baz
Content-Type: application/json;
{
"filename": "cloudx.jpg"
}
--foo_bar_baz
Content-Type: image/jpeg
{JPEG data}
--foo_bar_baz--
I'm building Android application and I need to write the request to send the image to the above WS. I was looking around for some time and I didn't found good resource to study this issue I have.
following may give you basic idea
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(<URI>);
HttpEntity entity = new FileEntity(file,ContentType.MULTIPART_FORM_DATA);
//there are other types of entities and content types too check documentation
req.setEntity(entity);
HttpResponse response = httpClient.execute(req);

How to emulate a browser HTTPS POST request with Java (Apache HTTP Client)?

There is a website with an AJAX API. I have opened Firebug to look into the details of the login HTTPS POST request.
Then I have tried to do the same POST request from my Java program using Apache HTTP Client. But somehow the server identified my request as a non browser request. It sends a security exception message, which tells me that.
When all request headers are the same, what else could identify my client as not a browser?
My guess is that it's a cookie issue (e.g. JSESSIONID the browser has stored). Include the session information with your POST. Have a look at the cookies of this site. Try disabling cookies for this site a have a look a the request again.
user-agent header? "httpclient.useragent" property
Use debug mode to see full wire logging and compare the request with firebug's one.
Dont know about the POST request but there is this for a multipart request
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
see if its of any help
EDIT: Code sample for a multipart request
String createOrderUrl = Constants.CREATE_ORDER_URL;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(createOrderUrl);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
// add the information to the multipart request
entity.addPart("msisdn", new StringBody("something"));
entity.addPart("recipientname", new StringBody("something"));
entity.addPart("recipientnumber", new StringBody("something"));
entity.addPart("recipientaddress", new StringBody("something"));
// add the images
for (String imagePath : selectedImages)
{
FileBody bin = new FileBody(new File(imagePath));
entity.addPart("image", bin);
}
httpPost.setEntity(entity);
return httpClient.execute(httpPost);

Categories