AW: How to work with HttpPut?

2017-01-30 Thread Schurr, Bastian
Hi,
thanks to Bernd - using EntityBuilder its working now.

Solution:
HttpEntity reqEntity = EntityBuilder.create().setFile(fIn).build();

Uploaded file:
--file: 
Walze.txt---
line 1: content
line 2: content
--file: 
Walze.txt---

Regards, Bastian

-Ursprüngliche Nachricht-
Von: Bernd Eckenfels [mailto:e...@zusammenkunft.net] 
Gesendet: Freitag, 27. Januar 2017 09:06
An: httpclient-users@hc.apache.org
Betreff: Re: How to work with HttpPut?

Hello,
You are using a multipart entity as you want to upload a form. Since the server 
stores all headers I guess it is expecting a simple entity with the raw content 
instead. (I think EntityBuilder.setFile())

Gruss
Bernd
--
http://bernd.eckenfels.net

_
From: Schurr, Bastian 
Sent: Freitag, Januar 27, 2017 8:19 AM
Subject: How to work with HttpPut?
To:  


Hi,
I have to upload files - text files as well as binary files - to a server using 
http put method. I get the URL from the server and upload my sample files to 
the server without any failure.
But when I open the uploaded files on the server I find modifications in the 
files like follows (original content in green):

--file: 
Walze.txt---
--359OQxCzKReuMiQUCDjZkKneprgsPnZApeFpvoPF
Content-Disposition: form-data; name="Walze.txt"; filename="Walze.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

line 1: content
line 2: content
--359OQxCzKReuMiQUCDjZkKneprgsPnZApeFpvoPF--
--file: 
Walze.txt---

Here is my code:

public List put(File fIn, String sUrl) throws IllegalArgumentException, 
ClientProtocolException, IOException {
if (fIn != null && sUrl != null && !sUrl.isEmpty()) {

   CloseableHttpClient client = 
HttpClients.createDefault();
   try {
   HttpPut request = new 
HttpPut(sUrl);

   FileBody file = new 
FileBody(fIn);

   HttpEntity reqEntity = 
MultipartEntityBuilder.create().addPart(fIn.getName(), file).build();
   request.setEntity(reqEntity);

   CloseableHttpResponse response = 
client.execute(request);

   List vLines = 
this.readResponse(response);

   this.handleResponse(response);

   return vLines;
   } finally {
   try {
   client.close();
   } catch (IOException eIgnore) {
   }
   }
}
}

private List readResponse(CloseableHttpResponse response) throws 
IOException {
List vLines = new ArrayList();

HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
   BufferedReader reader = new BufferedReader(new 
InputStreamReader(resEntity.getContent()));

   String s;
   while (null != (s = reader.readLine())) {
   if (!s.isEmpty()) {
   vLines.add(s);
   }
   }
}
return vLines;
}

private void handleResponse(CloseableHttpResponse response) throws IOException {
Logger.getLogger(this.getClass()).debug("Response status line 
[" + response.getStatusLine() + "]");
Logger.getLogger(this.getClass()).debug("Response status code 
[" + response.getStatusLine().getStatusCode() + "]");
Logger.getLogger(this.getClass()).debug("Response status phrase 
[" + response.getStatusLine().getReasonPhrase() + "]");

Header[] vHeaders = response.getAllHeaders();
for (Header header : vHeaders) {
   String sName = header.getName();
   String sValue = header.getValue();
   
Logger.getLogger(this.getClass()).debug("Response header " + sName + " [" + 
sValue + "]");
}

HttpEntity resEntity = response.getEntity();
 

Re: Support HTTP/2 protocol

2017-01-30 Thread Shawn Heisey
On 1/26/2017 8:15 AM, Oleg Kalnichevski wrote:
> ALPN will be supported as soon as it is supported by the Java platform
> (which is not going to happen until Java 9). 

I see evidence that the other Java http implementations have ALPN
support already ... but those systems implement both server and client. 
Could it be that those ALPN implementations are server-side only?  I
can't seem to easily locate anything saying for sure.

> ALPN can be used to advertise server protocol capabilities at the time
> of SSL handshake and allow clients to pick the desired protocol from
> the list of supported protocols. If one knows supported protocols
> beforehand ALPN is completely useless. Clients can go straight to using
> HTTP/2 if the server is known to support it.
>
> In the next release of HttpCore I would like to add protocol detection
> logic to enable endpoints to detect HTTP protocol version by examining
> the first packet received from the opposite endpoint. This in my
> opinion would be a much more practical feature. ALPN presently is very
> low on my priority list.
>
> Having said that ALPN support contribution would be welcome if someone
> is willing to develop it.

Knowing in advance that HTTP/2 support is available may be problematic. 
I can imagine a situation where servers are upgraded gradually, and the
client may not know whether the one it's connecting to can support the
new protocol.  Can HTTP/2 detection be reliable without ALPN, even in
situations where connecting to the same host/port may support HTTP/2 on
one connection, but not the next?  TCP load balancing is relatively
common with SSL.  If such detection can be reliable, then there won't be
anything to worry about.

For my webserver installations, I am hoping to get HTTP/2 support
enabled in the load balancer and worry about support on the back end
later.  I'm expecting the back-end LAN to be fast enough that multiple
connections can easily be established while the Internet-facing side
works through the inherent packet latency.

Thanks,
Shawn


-
To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
For additional commands, e-mail: httpclient-users-h...@hc.apache.org