I am using Junit for testing a REST webservice using Jersey API. I want to send the Content-Length header as my target host needs it.
If I try to specify content-length (request.header("Content-Length","475")), I get the following error,
Exception is: org.apache.http.client.ClientProtocolException
Caused by: org.apache.http.ProtocolException: Content-Length header already present
I would like to understand that if content-Length is already present implicitly, then why is the target-host not receiving it. How do I send the info in the request. When I do not specify the header, I get the following error from the host.
ApacheHttpClient4 client = HttpUtils.getClient();
WebResource wr = client.resource("https://myhost/myresourceendpoint")
ClientResponse cr = wr.header("Authorization","absxbsnsnsnsazd");
.post(ClientResponse.class,f) //f is the file containing the text
It has worked for me. I am getting the correct Content-Length at target. Also the attachment I am giving is a text file.
Author and all-around good cowpoke
Joined: Mar 22, 2000
Using available() may work correctly 1023 times, but if the operating system code reading the file happens to not have a buffer load and your last read exactly used the last available character, it will fail. See the JavaDocs.