Java Apache http library file upload issue - java

I Have a webservice that uploads a file. (details in the first link)
To communicate with this webservice I use org.apache.http library (Apache HttpComponents). I have found most of this code if not all of it here.
Sadly the solution only works with images and despite the efforts when trying to upload a video is shows the error of Content Length too long. To try and fix this I decided to replace it with what's in current use. At first using HttpClientBuilder
HttpClient httpclient = HttpClientBuilder.create().disableContentCompression().build();
My try failed as it still gives the same error and after research I found out that I need to use the CloseableHttpClient as follow to disable the automatic headers
CloseableHttpClient httpclient = HttpClients.custom()
.setHttpProcessor(HttpProcessorBuilder.create().build())
.build();
That worked but when setting the headers something doesn't pass I can't figure it out. I did it as follow
httppost.addHeader("Keep-Alive", "timeout=5, max=100");
httppost.addHeader("Connection", "Keep-Alive");
httppost.addHeader("Content-Type", "text/html; charset=UTF-8");
But when tracing the response this is what I have
Http Client Builder Trace
Date,Tue, 28 Mar 2017 18:30:50 GMT
Server,Apache/2.4.23 (Win64) PHP/7.0.10
X-Powered-By,PHP/7.0.10
Content-Length,43
Keep-Alive,timeout=5, max=100
Connection,Keep-Alive
Content-Type,text/html; charset=UTF-8
Closable Response Trace
Date,Tue, 28 Mar 2017 18:31:27 GMT
Server,Apache/2.4.23 (Win64) PHP/7.0.10
Content-Length,311
Connection,close
Content-Type,text/html; charset=iso-8859-1
I tried to set the headers to the response but I am haven't figured how to instantiate it properly to set the headers. And also I am trying to understand why the httppost is not being taken in consideration. Did I miss something?
One last thing when I use Closable The way I did it doesn't upload anymore due to missing headers I guess.
EDIT
Error when uploading video
Exception in thread "main" org.apache.http.ContentTooLongException: Content length is too long: 16363535
at org.apache.http.entity.mime.MultipartFormEntity.getContent(MultipartFormEntity.java:103)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:199)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:306)
at testfileupload.PostFile.main(PostFile.java:100)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
EDIT
I changed the max file size for php following this but it still shows the same error
Update
This is how I handle the multipart (I use multipartform)
HttpEntity resEntity = MultipartEntityBuilder.create()
.addBinaryBody("userfile", file, ContentType.create("video/mp4"), file.getName())
.build();
I rememeber reading something about it (not sure) And as for the php.ini params that need to be of higher value upload_max_filesize = 40M and post_max_size = 40M and memory_limit = 128M which I am kinda sure its enough (using wamp64).
I kept trying to udnerstand the content length issue since ..
I found out that the HttpEntity sets calculates the length and I checked the value its 2 bits off I believe but no matter the conenction type it just doesn't work and sticks to content length error.
I also tried
HttpClient httpclient = HttpClients.custom().setDefaultHeaders(Arrays.asList(CustomHeader)).build();
But still nothing. At this point I highly doubt its about setting the header and more related to HttpEntity with the Multipart or the HttpPost but It could be else.
Upload
I tried with a 9s Video of a size ~350Kb but it still gives the content length error which means there might be a default content set or none at all

So I dont know what I did / changed but now its working with the error.
I would really want to know why this cause an error it doesn't make sense so far unless its calling something that can't take too high of a value (method of param type given value more than it can)
To be clear the error is generated when I use this (verifcation area)
System.out.println(response.getStatusLine());
if (resEntity != null) {
// error from below, kinda forgot about it
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
// resEntity.consumeContent();
}
The webservice is the same as in the first link.
Below is my code (dont mind the comments I didn't clear the decrepated / changed / replaced / test / different approaches)
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpProcessorBuilder;
import org.apache.http.util.EntityUtils;
public class PostFile {
public static void main(String[] args) throws Exception {
// HttpClient httpclient = new DefaultHttpClient();
// CloseableHttpClient httpclient = HttpClients.custom()
// .setHttpProcessor(HttpProcessorBuilder.create().build())
// .build();
// Header headerCustom = new BasicHeader(HttpHeaders.CONTENT_LENGTH, "15");
// HttpClient httpclient = HttpClients.custom().build();
// HttpClient httpclient = HttpClientBuilder.create().disableContentCompression().build();
HttpClient httpclient = HttpClientBuilder.create().build();
// httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://localhost/userVids/upload.php");
httppost.setProtocolVersion(new ProtocolVersion("HTTP", 1, 1));
File file = new File("C:/Users/User/Desktop/test_video.mp4");
// httppost.addHeader("Content-Length", String.valueOf(file.length()));
// MultipartEntity mpEntity = new MultipartEntity();
// ContentBody cbFile = new FileBody(file, ContentType.MULTIPART_FORM_DATA);
// mpEntity.addPart("userfile", cbFile);
// System.out.println(mpEntity.getContentLength());
//
// httppost.setEntity(mpEntity);
/*
Date,Tue, 28 Mar 2017 18:30:50 GMT
Server,Apache/2.4.23 (Win64) PHP/7.0.10
X-Powered-By,PHP/7.0.10
Content-Length,43
Keep-Alive,timeout=5, max=100
Connection,Keep-Alive
Content-Type,text/html; charset=UTF-8
*/
/*
Date,Tue, 28 Mar 2017 18:31:27 GMT
Server,Apache/2.4.23 (Win64) PHP/7.0.10
Content-Length,311
Connection,close
Content-Type,text/html; charset=iso-8859-1
*/
HttpEntity resEntity = MultipartEntityBuilder.create()
.addBinaryBody("userfile", file, ContentType.create("video/mp4"), file.getName())
.build();
System.out.println("Entity Length " + resEntity.getContentLength());
// httppost.addHeader("Keep-Alive", "timeout=5, max=100");
// httppost.addHeader("Connection", "Keep-Alive");
// httppost.addHeader("Content-Type", "text/html; charset=UTF-8");
// httppost.addHeader("Content-Disposition", "form-data; name=\"userfile\"; filename=\"star_empty.png\"");
// httppost.addHeader("Content-Type", "image/png");
// httppost.addHeader("Content-Transfer-Encoding", "binary");
// httppost.addHeader("Content-Length", "99999");
httppost.setEntity(resEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
// HttpEntity resEntity = response.getEntity();
System.out.println("Headers (name,value)");
List<Header> httpHeaders = Arrays.asList(response.getAllHeaders());
httpHeaders.forEach((header) -> {
System.out.println(header.getName() + "," + header.getValue());
});
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
// resEntity.consumeContent();
}
// httpclient.getConnectionManager().shutdown();
}
}
BUT A HUGE NOTICE I had my php.ini (changed all of them in wamp didn't know if it would consider only the running version or reads all (didn't test) ) and changed the values as Lucas Holt (he fixed it i.e, I am just providing for who needs or maybe explanation) said and he deserves credit for it. (I am sure it didn't work before that I just tested a lot and dont remember really.)

Related

How to fix "Early EOF" error from java http post of a file?

I'm trying to post a file to a rest API and the code I'm using gives an "Early EOF" error. I've looked around and most of what I'm seeing seems to suggest that the data send doesn't match the expected size of what is being sent.
How do I fix this?
Code is shown below:
(based on How can I make a multipart/form-data POST request using Java?)
import java.io.File;
import java.io.FileInputStream;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import lombok.extern.slf4j.Slf4j;
#Slf4j
public class HttpFileUpload {
public static void main(String[] args) throws Exception {
log.info("Starting...");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("https://url-to-rest-api/api/2.0/dbfs/put");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("path", "/FileStore/my-dir/myfilename.txt", ContentType.TEXT_PLAIN);
String token = "my-security-token";
// This attaches the file to the POST:
File f = new File("C:\\path-to\\my-file.txt");
builder.addBinaryBody(
"file",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName());
HttpEntity multipart = builder.build();
uploadFile.addHeader("Authorization", "Bearer " + token);
uploadFile.setEntity(multipart);
log.info("Starting upload");
CloseableHttpResponse response = httpClient.execute(uploadFile);
log.info("Done with upload");
HttpEntity responseEntity = response.getEntity();
log.info("Got response");
log.info("Done.");
}
}
This turned out to be a timeout issue. The api I was writing to timed out the request and generated the EOF exception. The api had another call that allowed the file to be uploaded in segments so I modified my solution to use this call instead of trying to upload the entire file in one call.

Request MultiPart/Form-Data with ByteArray

i want to send a request includes bytearray as form-data. Everyone using "File" but i have just the "bytearray" and i don't want to use any path. My request in curl:
curl -F file=#file server
In java what i tried:
byte[] fileByte = Base64.decodeBase64(parameter);
ByteArrayInputStream myFile = new ByteArrayInputStream(fileByte);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("server");
multipartEntityBuilder.addBinaryBody("file", myFile, ContentType.APPLICATION_OCTET_STREAM, "filename");
HttpEntity multipart = multipartEntityBuilder.build();
httpPost.setEntity(multipart);
and i tried
multipartEntityBuilder.addBinaryBody("file", fileByte, ContentType.DEFAULT_BINARY, "filename");
//OR FileBody, ByteArrayEntity, InputStream or like thats
So, that methods not worked for me. How i can send the request successfully ?
EDIT: i used the ptsv2 with postman and result ->
when i send the same request and file with java the result ->
i think issue is certainly related to the ByteArray or InputStream. I must find the another Type for the my byte[] or right method for post in java with using the File type but without path.
I don't know what "server" is, but maybe the problem is with the server you are sending to. I ran your code, writing to a public HTTP POST test server available on the internet, and it works fine for me. Here's the code I ran:
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
class ListsTest {
public static void main(String[] args) {
byte[] fileByte = "Sample string data".getBytes(StandardCharsets.UTF_8);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://ptsv2.com/t/r7ypk-1613291354/post");
multipartEntityBuilder.addBinaryBody("file", fileByte, ContentType.DEFAULT_BINARY, "filename");
HttpEntity multipart = multipartEntityBuilder.build();
httpPost.setEntity(multipart);
try {
client.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
}
}
If you go to the target test server bucket:
https://ptsv2.com/t/r7ypk-1613291354
you can see what the full request looks like as received by the server, and see that the payload makes it over just fine as a file with the appropriate filename and contents. The file content portion of the request display looks like this:
Your alternate method worked equally well for me. I chose to post this version as it is simpler, not requiring you to wrap your byte array in an InputStream object.

Httpclient-4.5.2.jar setEntity showing "cannot find symbol" (JAVA - netbeans 8)

I am trying to use some code that I got from a website that has sports data served publically via an API (http://developer.fantasydata.com).
The site provide some sample JAVA code to make the http request. For some reason the setEntity method for the declared request (request) is showing a "cannot find symbol error.
package epl.fixtures.test.app;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class EPLFixturesTestApp {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
HttpClient httpclient = HttpClients.createDefault();
try
{
URIBuilder builder = new URIBuilder("https://api.fantasydata.net/soccer/v2/json/CompetitionDetails/EPL");
URI uri = builder.build();
HttpGet request = new HttpGet(uri);
request.setHeader("Ocp-Apim-Subscription-Key", "****************");
// Request body
StringEntity reqEntity = new StringEntity("{body}");
request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
The line causing the issue is the request.setEntity(reqEntity); line
Can anyone explain this to me please? I have all the relevant jar files from apache added to the project libraries directory.
Thanks
HttpGet does not have a setEntity method.
This makes sense, since the request body has no meaning in GET requests.
Only classes implementing HttpEntityEnclosingRequest have this method.
I don't know why the documentation uses it, but it seems to work when omitting those two lines (which look meaningless anyway). Code:
URIBuilder builder = new URIBuilder("https://api.fantasydata.net/soccer/v2/json/CompetitionDetails/EPL");
URI uri = builder.build();
HttpGet request = new HttpGet(uri);
request.setHeader("Ocp-Apim-Subscription-Key", "****************");
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}

Java - StackExchange get JSON

Here is my Java code to get some information from stackexchange.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class StackExchangeConnect {
private static final String URL = "http://api.stackexchange.com/2.2/answers?key=U4DMV*8nvpm3EOpvf69Rxw((&site=stackoverflow&fromdate=1396310400&todate=1398729600&order=desc&sort=activity&filter=default";
public static void main(String[] args) throws Exception {
StackExchangeConnect http = new StackExchangeConnect();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
}
// HTTP GET request
private void sendGet() throws Exception {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(URL);
// add request header
request.addHeader("Accept", "application/json");
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
}
I am getting following in my console.
Testing 1 - Send Http GET request
Response Code : 200
�
I tried writing the contents to file, and it's like
�\00\00\00\00\00\00�Z�n�H�Cy�;_�`df1��&���.F�ٔK���|I���eƒ�M�,GO�Ev��:U�_gE����_g���U����ʭ7�i�r5���t��]uUd�ʤZW����]���n\���l]�y�pW���[�uӬ�$���;�W��4�:��2��&��:�6�L�N8�2���)�,18?ԯ1Q���E�VMa�ի����_�1�-+���<\�̲}��sS��E��^�co�[W��{y;�:f�n�NgE}e�u��r��E�Ng�-+x8����{S���Uf���V�c!�`wY�xl�r݂n�����әY�w��J�.��quw�h��v\00�0!�`"�k𘀩��U����q�i�:��L�$C4�y���Se�6��Y�hȆ�%u�5��F�0b�̓�P�0��՘�b�*�x������b�����+λ�;/��M���?�����z>#2�1g^���S���ӏ9y��.�ͪ]�v!��F��h/��&�/�I^D3ċ!��6dI�0\00l�a� �S���B��8�q�P���h� �H<�ݼ��g�^|.����y���G/���c�\00u2a���LQ�����_ņ=ů7w�A?�Ĵ>�$%X� �͇f��Ib�%AA�_8���T����(C�����S�,�Y��A�nYu<K��$��1y��(\00Rg�E�6��Lx�B#���d&x#�->����(?^�pu�^M�粹6����-0�X#�q'���l9���'��~`�&J�}`\'"E,�ڦFJJ�^�>�H*r�q��ܦVZ�X�[ vtzKAn��g�nG ���!�z��:HJ:�oPH�oiFH�m11rt���L#i\00$όu� 'J��+�,KU4fs[5e������vh�� h��K+��4�T>�!*,���JH2.��>$�rC�)�9�!�3k��eee�O��2�`�N�ݯ]���~^ɦ}#��g��>rM��LT�Ѡ\00�m�8�Z�&�z����L�]�j.Ք�8Z�g�3(!�U�a��D\�#��Y�G�m�^lV��&�o��G�cGu���e|���8��(:\9��D&,�T������O���8y�����OQ��|�L�{\00PR�l>\000�d�"����!J�Pa%A�vs����!��j�2�p�3ąS�aĴ��M���&�B�A���9,Dx���b�,�)�&��mI�2]��g9M2F K&s�5��+����5���++xc4\�Ԓy��|ĘV�l�2b�3���D%��m!`㇇7�f}}���K��[(�4���d]�fS���^�~��ֿ}����=��ɶ��kY(�"��|]p��m�2�4DA�����3$��tѩ�j*�T�Y��� Џ.齦W�lzٹ%���Y �(d�#���P���m���{�-"���T)�(IS�n��)}��!I����bq#���O��1a*��0ؼ�("��C}9�<LNۖ�B0�ڮС�qŹ�0�e�N%#.�X ���F�+�kS�h�3K�i��$bd"��#H�H5�)F=�DL�r�r���E�������<ٶ^e�{8�x���(oب�F��� g���]�B�l�6/��Q�gR`O�g��p%1s�I[�G���Ooq�J8�*(m榮��ӏ9�TP�������ᬳ�Z�w[1{��P���`�s�Kn���HG�3�p��Kᣦ����$2�9v1f5U����Ɲ�x�!~K��[����c�Li�˽ͧҰ�|�G(x�J��)��vk��N{s��?~{�F�N>�|�/�G�q������\uF�OƵ���<}��؇q�٭�p�K�8�RN��I�\00�b�" W,�q$�z��[v��ɶ�1hu�\00Z���"�G�c�����C"���> ��#�(ÿ��4��$���e$%qb)C9�^������r��h��i%�����`�!��Y�b�;<B���f;�P�����쮎7��r�{A�Ru��ܬ6l&t�M��ۤ^�ӊ�0���$B���C�)Ti{��)9�� 刋�U�ζ��,�Tk����'������o�u�L6��Y=x?_�I.C���\ ����S�"0�����Fi�d�f{V�R��V���j#e�s-�Ny�s�3���.���4��X�7:�&u����H%oy��km�(U�O����\B�?N<x�)���j_L'���[%�5ÔZ��J�4��9�hR�\����0�d����M��櫐8�T�P��}�aE[%N<�"v>a��������[�T�ɫ�nV��L|nw��=�,��f�����X��:�]�!�,�� hƩ<t�`�CT��r =O�CiNS��pim�����-��T7����#'�֩��({=>�K���C1<���Ngצ�Zv#꿡?7ec����#�o�Tni�U���.�C���Ý�=1\00\00
When I try the same URL in browser it's getting the JSON. If I try other URL in program, it's getting the JSON properly. Why is that?
And also, is there any Java API so that the JSON String I can convert to some Object?
NOTE:
Apache HTTP Client - 4.3.3, Apache HTTP Core - 4.3.2, Commons logging - 1.1.3
UPDATE 1
I am printing response. System.out.println(response.toString()); And I am getting the following output.
HTTP/1.1 200 OK [Cache-Control: private, Content-Type: application/json; charset=utf-8, Content-Encoding: gzip, Access-Control-Allow-Origin: *, Access-Control-Allow-Methods: GET, POST, Access-Control-Allow-Credentials: false, Date: Tue, 29 Apr 2014 06:31:45 GMT, Content-Length: 2551]
It looks like it's compressed, w hich should be handled by HTTPClient; try using the builder and disabling compression.
The content being returned is gzip compressed. you can see this in your output of the response:
... Content-Encoding: gzip ...
Given that you're using the 4.3.3 version of the Apache HTTP client library, you simply need to do the following:
HttpClient client = HttpClientBuilder.create().build();
This will construct a HttpClient that has no problem decompressing the content, and you will get the JSON you're expecting ( The DefaultHttpClient does not support compression.)
The content is gzipped. You can verify that using
System.out.println(connection.getContentEncoding());
To get the content, use something like this.(This is java.net.URl version. Please change accordingly for httpclient)
GZIPInputStream gs = new GZIPInputStream(connection.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(gs));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
You should render the response as UTF-8.
And your second question is the Jackson library. It can conver json objects into Java objects.

How to specify the target URL path in HTTP Proxy authentication

I need to get the API response contents from an API server to my local server, via a proxy authentication. The API request will be like:
http://api.example.com/nav_page/head?locale=in&pointer=old&cont=/resque/&scope=old&release=new
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ClientProxyAuthentication {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("proxy.example.com", 8080),
new UsernamePasswordCredentials("Domain\user", "pass"));
HttpHost targetHost = new HttpHost("api.example.com", 80, "http");
HttpHost proxy = new HttpHost("proxy.example.com", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet httpget = new HttpGet("/");
System.out.println("executing request: " + httpget.getRequestLine());
System.out.println("via proxy: " + proxy);
System.out.println("to target: " + targetHost);
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
System.out.println(EntityUtils.toString(entity));
}
Header[] headers = response.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
In this code, if target URL is specified as api.example.com, The authentication succeeds and getting the resource (but there is no resource in base dir, hence nothing useful received). But to get correct response, I need to provide the navigation path inside that host.
Hence if i append the path like
HttpHost targetHost = new HttpHost("api.example.com/nav_page/head?locale=in&pointer=old&cont=/resque/&scope=old&release=new", 80, "http");
I am getting some error response, but not correct one.
HTTP/1.1 503 Service Unavailable
Response content length: 442
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Temporarily Unavailable</title>
</head><body>
<h1>Service Temporarily Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
I am able to get the response, if i hit URL directly in browser.
May i know how to send the request for the navigation path?

Categories