-1

I have this code in a method which is uploading a file using POST-method:

HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

and then some other non-deprecated logic (will provide if needed, can't copy and paste). I've read some of the posts and am trying to use CloseableHttpClient, but I can't come up with an idea how to update the code. I've just started working on this project and am really inexperienced with it.

This method uploads a file and receives a response from the server. My question is - how do I write the same code without using deprecated methods?

Kirill
  • 63
  • 11
  • 1
    Where is the question? – Rafał Spryszyński Jul 19 '17 at 08:32
  • I need to get rid of deprecated methods altogether, these are the only deprecated methods I haven't fixed yet, so the question is - how do I write the same code without using deprecated methods? – Kirill Jul 19 '17 at 08:36
  • What are the deprecated methods you're using that you want to stop using? and what does the Javadoc have to say about them? – user207421 Jul 19 '17 at 09:08
  • Javadoc suggests using HttpClientBuilder, which I'm trying to use right now. When I run the test, this method seems to be running pretty smooth without setting any params. So yeah, I've found an answer, but now I'm curious how is HttpClient.create().build() work the same as my given code – Kirill Jul 19 '17 at 09:26

1 Answers1

0

Try this:

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
HttpClient httpClient = httpClientBuilder.build();

This should be the "new" way to do this, according to the API

with httpClient.execute(/*params*/); you should be able to run default as well as custom contexts. See here

Another possible way:

HttpPost httpPost = new HttpPost("/");
httpPost .setProtocolVersion(HttpVersion.HTTP_1_1);
Naeramarth
  • 112
  • 13
  • I've solved the problem using this suggestion https://stackoverflow.com/questions/15336477/deprecated-java-httpclient-how-hard-can-it-be which is pretty much the same as yours, thanks :) – Kirill Jul 19 '17 at 09:30