post can't reach on target with Heroku - Fixie - java

I'm making REST API service with another REST API service.
When I post a request to use external REST API service, the post can't reach on target.
The target host requires static IP address and HTTP protcol, so I use Fixie on Heroku.
I get following messages on heroku.
o.apache.http.impl.execchain.RetryExec : I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {tls}->http://xxxxx.usefixie.com:80->https://my-target-host.com:443: The target server failed to respond
o.apache.http.impl.execchain.RetryExec : Retrying request to {tls}->http://xxxxx.usefixie.com:80->https://my-target-host.com:443 org.apache.http.NoHttpResponseException: my-target-host.com:443 failed to respond
Currentry, the host don't expose access log.
These are my Java code.
Code-A.
HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-target-host.com",443,"https");
post.setHeader("Content-Type", "application/json; charset=UTF-8");
try{
URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
HttpRoutePlanner routePlanner = new HttpRoutePlanner() {
#Override
public HttpRoute determineRoute(HttpHost target, org.apache.http.HttpRequest request,
org.apache.http.protocol.HttpContext context) throws HttpException {
return new HttpRoute(target, null, new HttpHost(proxyUrl.getHost(), proxyUrl.getPort()), true);
}
};
try (CloseableHttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();) {
try {
post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
CloseableHttpResponse res = httpclient.execute(targetHost,post);
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
Code-B.
HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-target-host.com",443,"https");
post.setHeader("Content-Type", "application/json; charset=UTF-8");
try{
URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
String userInfo = proxyUrl.getUserInfo();
String user = userInfo.substring(0, userInfo.indexOf(':'));
String password = userInfo.substring(userInfo.indexOf(':') + 1);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUrl.getHost(),proxyUrl.getPort()),
new UsernamePasswordCredentials(user,password));
try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();) {
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
post.setConfig(config);
String encodedAuth = Base64.getEncoder().encodeToString(userInfo.getBytes());
post.setHeader("Proxy-Authorization", "Basic " + encodedAuth);
try {
post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
CloseableHttpResponse res = httpclient.execute(targetHost,post);
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
Code-C.
HttpPost post = new HttpPost("/action");
HttpHost targetHost = new HttpHost("my-tareget-host.com",443,"https");
try{
URL proxyUrl = new URL(System.getenv("FIXIE_URL"));
String userInfo = proxyUrl.getUserInfo();
String user = userInfo.substring(0, userInfo.indexOf(':'));
String password = userInfo.substring(userInfo.indexOf(':') + 1);
DefaultHttpClient httpclient_ = new DefaultHttpClient();
try {
httpclient_.getCredentialsProvider().setCredentials(
new AuthScope(proxyUrl.getHost(), proxyUrl.getPort()),
new UsernamePasswordCredentials(user, password));
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort());
httpclient_.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
String encodedAuth = Base64.getEncoder().encodeToString(userInfo.getBytes());
post.setEntity(new StringEntity(CONTENT_JSON, StandardCharsets.UTF_8));
post.setHeader("Proxy-Authorization", "Basic " + encodedAuth);
HttpResponse rsp = httpclient_.execute(targetHost, post);
} finally {
httpclient_.getConnectionManager().shutdown();
}
}catch(Exception e){
e.printStackTrace();
}
These code cause same result.
On same environment, this ruby code is reached.
RestClient.proxy = ENV['FIXIE_URL'] if ENV['FIXIE_URL']
RestClient.post("https://my-target-host.com/action", CONTENT_JSON, {
'Content-Type' => 'application/json; charset=UTF-8'
})
What's happend on access? What should I modify on Java?

Related

httpclient3.1 can request data but httpclient4.x cannot

Why 3.1 can request data normally, but using 4.5 will throw Circular redirect
Do I need any additional configuration to make it compatible with 3.1?
In 4.5 I set setCircularRedirectsAllowed and setMaxRedirects but it still throws an exception
httpclient3.1:
private static Document requestData(String url, CookieStore cookieStore, String proxyIp, String proxyPort, String userAgent) {
HttpClient hc = new HttpClient();
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"));
headers.add(new Header("Accept-Language", "zh-CN,zh;q=0.9"));
headers.add(new Header("Cache-Control", "max-age=0"));
headers.add(new Header("connection", "keep-alive"));
StringBuilder tmpcookies = new StringBuilder();
for (Cookie cookie : cookieStore.getCookies()) {
tmpcookies.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
headers.add(new Header("Cookie", tmpcookies.toString()));
headers.add(new Header("Host", "Host"));
headers.add(new Header("Upgrade-Insecure-Requests", "1"));
headers.add(new Header("User-Agent", userAgent));
hc.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
hc.getHostConfiguration().setProxy(proxyIp, Integer.parseInt(proxyPort));
hc.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
hc.getHttpConnectionManager().getParams().setSoTimeout(10000);
GetMethod get = new GetMethod(url);
try {
int code = hc.executeMethod(get);
if (code == 200) {
InputStream inputStream = get.getResponseBodyAsStream();
String result = StringUtil.inputStreamToString(inputStream);
return Jsoup.parse(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
httpclient4.x :
private static Document requestData(String url, CookieStore cookieStore, RequestConfig defaultRequestConfig, String userAgent, String uniSocCreCode) {
CloseableHttpClient hc = HttpClients.custom().build();
HttpGet get = new HttpGet(url);
get.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
get.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
get.addHeader("Cache-Control", "max-age=0");
get.addHeader("connection", "keep-alive");
StringBuilder tmpcookies = new StringBuilder();
for (Cookie cookie : cookieStore.getCookies()) {
tmpcookies.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
}
get.addHeader("Cookie", tmpcookies.toString());
get.addHeader("Host", "Host");
get.addHeader("Upgrade-Insecure-Requests", "1");
get.addHeader("User-Agent", userAgent);
get.setConfig(defaultRequestConfig);
try {
CloseableHttpResponse response = hc.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
InputStream inputStream = response.getEntity().getContent();
String result = StringUtil.inputStreamToString(inputStream);
return Jsoup.parse(result);
}
} catch (Exception e) {
log.error("error", e);
}
return null;
}
error(4.x):
Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'https://www.qcc.com/firm/a4eb893fb989c8cdd7a73809d5b65715.html'
at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:193)
at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:223)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:126)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
... 68 common frames omitted
Still think it is a version problem
move to org.apache.httpcomponents.client5 » httpclient5 solved the problem

Http POST + auth using Apache async http client

I am doing a asynchronous http request to a web service. I am not that sure if this is the correct way to do it but it works.
Is this the correct way to make POST + authentication with HttpAsyncClient?
Should I close the httpclient at the end with httpclient.close(); ?
public void asyncHttpRequest() {
try {
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3000)
.setConnectTimeout(3000).build();
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.build();
httpclient.start();
String postParameter = new JSONObject().put("key", "value").toString(); //Creating JSON string
HttpPost httpPost = new HttpPost("http://www.url.com");
httpPost.setEntity(new StringEntity(postParameter));
UsernamePasswordCredentials creds
= new UsernamePasswordCredentials("username", "password");
httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpclient.execute(httpPost, new FutureCallback<HttpResponse>() {
#Override
public void completed(final HttpResponse response) {
try {
InputStream responseBody = response.getEntity().getContent();
String serverResponse = IOUtils.toString(responseBody);
System.out.println("Server response : " + serverResponse);
System.out.println(httpPost.getRequestLine() + "->" + response.getStatusLine());
} catch (IOException | UnsupportedOperationException ex) {
//Do something
}
}
#Override
public void failed(final Exception ex) {
//Do something
}
#Override
public void cancelled() {
//Do something
}
});
} catch (IOException ex) {
//Do something
} catch (AuthenticationException ex) {
//Do something
}
}
Any help is appreciated!
I would suggest using a credentials provider like below, instead of explicitly adding a header for basic authentication:
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials creds =
new UsernamePasswordCredentials("username", "password");
provider.setCredentials(AuthScope.ANY, creds);
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(requestConfig)
.setDefaultCredentialsProvider(provider)
.build();
Also, it would be best to explicitly close the httpclient after the request has been completely processed.

Android HttpsURLConnection - simple POST request with parameters

I've been trying to get a simple android client server app working, and I've had nothing but trouble. I'm hoping someone can look at this code and see what I'm doing wrong, maybe I'm doing things in the wrong order, or forgetting something? Just adding the relevant parts
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "application/json");
// set some made up parameters
String str = "{'login':'superman#super.com','password':'password'}";
byte[] outputInBytes = str.getBytes("UTF-8");
OutputStream os = conn.getOutputStream();
os.write( outputInBytes );
os.close();
// connection.setDoOutput(true); //should trigger POST - move above -> crash
conn.setRequestMethod("POST"); // explicitly set POST -move above so we can set params -> crash
conn.setDoInput(true);
The error I get is
'exception: java.net.ProtocolException: method does not support a
request body: GET'
If I just do a POST request without parameters it's fine, so I guess I should move the connection.setDoOutput(true); or conn.setRequestMethod("POST"); higher up, that should work right? When I do that I get the error:
exception: java.net.ProtocolException: Connection already established.
So, if I try to set to POST before adding parameters it doesn't work, if I try to do it after it doesn't work... what am I missing? Is there another way I should be doing this? Am I adding parameters incorrectly? I've been searching for a simple android networking example, and I can't find any, is there any example the official Android site? All I want to do is a very basic network operation, this is so frustrating!
EDIT: I need to use HttpsURLConnection for reasons not included in the above code- I need to authenticate, trust hosts, etc- so I'm really looking for a potential fix for the above code if possible.
Here is an example of how to post with a JSON Object:
JSONObject payload=new JSONObject();
try {
payload.put("password", params[1]);
payload.put("userName", params[0]);
} catch (JSONException e) {
e.printStackTrace();
}
String responseString="";
try
{
HttpPost httpPost = new HttpPost("www.theUrlYouQWant.com");
httpPost.setEntity(new StringEntity(payload.toString()));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(httpPost);
responseString = EntityUtils.toString(response.getEntity());
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
And example of how to get
String responseString = "";
//check if the username exists
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("www.theUrlYouQWant.com");
ArrayList<String> existingUserName = new ArrayList<String>();
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
else
{
Log.e(ParseException.class.toString(), "Failed to download file");
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
I followed this tutorial on making http calls:
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
Works fine with no problems.
Below is a class that I have modified from the sample:
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
String TAG = ((Object) this).getClass().getSimpleName();
public ServiceHandler() {
}
/**
* Making service call
*
* #url - url to make request
* #method - http request method
*/
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
*
* #url - url to make request
* #method - http request method
* #params - http request params
*/
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 2000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 2000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
Log.e("Request: ", "> " + url);
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
if (httpResponse != null) {
httpEntity = httpResponse.getEntity();
} else {
Log.e(TAG, "httpResponse is null");
}
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
And this is how I use the class:
nameValuePairs = new ArrayList<NameValuePair>();
String param_value = "value";
String param_name = "name";
nameValuePairs.add(new BasicNameValuePair(param_name, param_value));
// Creating service handler class instance
sh = new ServiceHandler();
String json = sh.makeServiceCall(Utils.getUrl, ServiceHandler.GET, nameValuePairs);

Sending POST data to website and getting answer

I have a problem with getting direct link to video. I want to play it in my WebView/VideoView. How to send POST request and recieve answer with direct link from website which decode such things:
videotools.12pings.net
Is there any way to do that?
Example: put link in the website form - than click a button - direct link is ready under the button
final HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is
// established.
HttpConnectionParams.setConnectionTimeout(httpParameters, 7000);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setSoTimeout(httpParameters, 10000);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpResponse response = client.execute(new HttpGet(path));
HttpEntity entity = response.getEntity();
InputStream imageContentInputStream = entity.getContent();
path is the variable that contains your URL
I hope this will help you..
*You need to get
httpcomponents-client-4.1.zip and apache-mime4j-0.6.1-bin.zip
Add
apache-mime4j-0.6.1-bin.zip
and
httpclient-4.1.jar
httpcore-4.1.jar
httpmime-4.1.jar
from the lib folder in httpcomponents-client-4.1.zip
- See more at: http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/#sthash.N7qT8apH.dpuf*
try {
MultipartEntity multipart = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
FormBodyPart office = new FormBodyPart("office",
new StringBody(getOffice));
multipart.addPart(office);
String imageCount = Integer.toString(drawableList.size());
System.out.println("ImageCount : " + imageCount);
FormBodyPart imgNo = new FormBodyPart("imgNo", new StringBody(
imageCount));
multipart.addPart(imgNo);
} catch (Exception e) {
// TODO: handle exception
}
try {
System.out.println("result : " + multipart.getContentLength());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(CommunicatorUrl.ADD_INCIDENT);
httppost.setEntity(multipart);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print responce
outPut = EntityUtils.toString(entity);
} catch (Exception e) {
Log.e("log_tag ******",
"Error in http connection " + e.toString());
}
basically this MultipartEntity is useful for sending multiple images and datas to server using post method
String paramUsername = "username";
String paramPassword = "password";
System.out.println("*** doInBackground ** paramUsername " + paramUsername + "
paramPassword :" + paramPassword);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.xxxxxxxxxxxxxx.php");
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("ParamUsername", paramUsername);
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("paramPassword", paramPassword);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println("First Exception caz of HttpResponese :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Second Exception caz of HttpResponse :" + ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
uee.printStackTrace();
}

HTTP requests with basic authentication

I have to download and parse XML files from http server with HTTP Basic authentication. Now I'm doing it this way:
URL url = new URL("http://SERVER.WITHOUT.AUTHENTICATION/some.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
But in that way I can't get xml (or I'm just simply not aware of that ) document from server with http authentication.
I will be really grateful if you can show me the best and easiest way to reach my goal.
You can use an Authenticator. For example:
Authenticator.setDefault(new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"user", "password".toCharArray());
}
});
This sets the default Authenticator and will be used in all requests. Obviously the setup is more involved when you don't need credentials for all requests or a number of different credentials, maybe on different threads.
Alternatively you can use a DefaultHttpClient where a GET request with basic HTTP authentication would look similar to:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://foo.com/bar");
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "password"),
"UTF-8", false));
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();
// read the stream returned by responseEntity.getContent()
I recommend using the latter because it gives you a lot more control (e.g. method, headers, timeouts, etc.) over your request.
public String reloadTomcatWebApplication(String user, String pwd, String urlWithParameters, boolean returnResponse) {
URL url = null;
try {
url = new URL(urlWithParameters);
} catch (MalformedURLException e) {
System.out.println("MalformedUrlException: " + e.getMessage());
e.printStackTrace();
return "-1";
}
URLConnection uc = null;
try {
uc = url.openConnection();
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
e.printStackTrace();
return "-12";
}
String userpass = user + ":" + pwd;
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
uc.setRequestProperty("Authorization", basicAuth);
InputStream is = null;
try {
is = uc.getInputStream();
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
e.printStackTrace();
return "-13";
}
if (returnResponse) {
BufferedReader buffReader = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
String line = null;
try {
line = buffReader.readLine();
} catch (IOException e) {
e.printStackTrace();
return "-1";
}
while (line != null) {
response.append(line);
response.append('\n');
try {
line = buffReader.readLine();
} catch (IOException e) {
System.out.println(" IOException: " + e.getMessage());
e.printStackTrace();
return "-14";
}
}
try {
buffReader.close();
} catch (IOException e) {
e.printStackTrace();
return "-15";
}
System.out.println("Response: " + response.toString());
return response.toString();
}
return "0";
}
Use HttpClient. Documentation for performing downloads with HTTP AUTH is here. Documentation for getting a string result is here. Then, parse your string (ideally using SAX, though, not DOM).
DefaultHttpClient deprecated
addHeader must have 2 parameters
Updated code block using HttpClient 4.5.2
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet("https://test.com/abc.xyz");
httpGet.addHeader("Authorization", BasicScheme.authenticate(new UsernamePasswordCredentials("login", "password"), "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();
As Gabe Rogan mentioned, "The method authenticate from BasicScheme has been deprecated".
An alternative way to do this,
HttpRequestBase hrb = new HttpGet(req.getUrl()); // should be your URL
UsernamePasswordCredentials Credential= new UsernamePasswordCredentials("id", "password");
Header header = new BasicScheme(StandardCharsets.UTF_8).authenticate(Credential, hrb, null);
hrb.addHeader(header);

Categories