I have googled but many concerns was "java.lang.IllegalStateException: Target host must not be null", but in my case error was "java.lang.IllegalStateException: Target host is null"
I am trying to post a request using following code
StringEntity reqContent = new StringEntity(xmlData);
reqContent.setContentType("text/xml");
HttpPost req = new HttpPost(serverURL);
req.setEntity(reqContent);
httpClient = new DefaultHttpClient(connMgr, params);
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
HttpResponse response = httpClient.execute(req);
After executing the request I am getting "Target host is null".
I am giving a valid host. Does this error comes when Target host which I am trying to access is not available?
When sending a request , somehow my URL which I want to send the request to is getting null which caused this issue when I am trying to execute a request.
Related
I have a task to ping and endpoint using a post request. The issue I am having is the full endpoint is not clearly defined. I have a docker image and container but I am not sure how to use it in order to make a successful api call.
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("/auth");
//Auth object just has 1 field called account
Auth a = new Auth("Test-acc");
Gson gson= new Gson();
String json = gson.toJson(a);
System.out.println("Json: " + json);
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Api-Key", "API Key is given for Test-acc");
CloseableHttpResponse response = client.execute(httpPost); //error as unknown endpoint?
client.close();
//read response
HttpEntity res = response.getEntity();
String result = EntityUtils.toString(res);
The error I get is: client.ClientProtocolException. The endpoint /auth gives me back a session token but I am unsure how to call the full endpoint (e.g. https://.../auth). I assume it has something to do with the docker container that is provided.
To test that the container is running I run docker run and get the response {"message": "ok"}. But other than that, I am unsure how to use it. Will the image in the container help with this?
Docker run command:
docker run -p 7902:7902 tesingCompany/testprovider
You have two choices :
You lanch this code from another container on the same user defined docker network, in this case the host is the container name
You launch this code from your host and you need to link a port from your host to a port on your container. You do that when you launch your container with the -p 7092:7092 option. It means your host 7092 port is linked to your container 7092, and you can contact it with localhost:7092
I am trying to send a HTTP POST request to a site with a HTTPS proxy.
I am currently doing it like that:
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
HttpPost request = new HttpPost("https://example.com");
HttpHost proxy2 = new HttpHost("proxy ip here", 8080, "https");
RequestConfig config = RequestConfig.custom()
.setProxy(proxy2)
.build();
request.setConfig(config);
String json = "\"" + username + "\"";
StringEntity entity = new StringEntity(json);
request.setEntity(entity);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
CloseableHttpResponse response = httpclient.execute(request, context);
HttpEntity entityresponse = response.getEntity();
responseString = EntityUtils.toString(entityresponse, "UTF-8");
response.close();
httpclient.close();
But I am getting this: java.net.SocketException: Connection reset
I've tried a lot of proxies and different URLs too but the same problem is there.
It work fine if I set a HTTP URL and the http parameter in the proxy host line, but I want HTTPS :/
Any help would be appreciated.
Thanks!
If you want to call https url, you must have to install the certificate in your jre security/lib folder.
In order to install the certificate, please follow below steps:
Download InstallCert.java file from : https://confluence.atlassian.com/download/attachments/180292346/InstallCert.java?version=1&modificationDate=1315453596921
copy InstallCert.java to any location.
run: javac InstallCert.java
run: java InstallCert example.com:port
jssecacerts file will be generated
Copy it inside JAVA_HOME\jre\lib\security folder
I hope this should resolve your issue.
I try to do a http request from my server and want to use different ip addresses (do one request with one IP and another with another IP). I read that it should work with the http client of apache. This is the code i use:
HttpClientBuilder builder = HttpClients.custom();
Builder configBuilder = RequestConfig.custom();
configBuilder.setLocalAddress(InetAddress.getByAddress(new byte[] {85,2,(byte) 246,4}));
CloseableHttpClient httpClient = builder.setDefaultRequestConfig(configBuilder.build()).build();
HttpGet httpGet = new HttpGet("http://xy.com/");
CloseableHttpResponse response1 = httpClient.execute(httpGet);
//do something with the response
response1.close();
resulting in the following exception:
Exception in thread "main" java.net.BindException: Cannot assign requested address: JVM_Bind
at the line "CloseableHttpResponse response1 = httpClient.execute(httpGet);"
What am I doing wrong? (It don't has to be done with apache, it just needs to be java. Best would be Play Framework because I normally do my request with that but I just want it to work, anything else is secondary)
Thanks
I'm writing a network android application that uses http requests to get data. The data is HTML format. I use Apache HttpClient and JSoup.
When I'm out of traffic with my mobile internet provider, I am always redirected to the providers' page saying that I should pay some money. Of course, it is a bad idea to parse this page.
How to detect occured page substitution?
This code will help you to know with is the final target of your request, if isn't the page that you asked for, is the provider page.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://www.google.com/");
HttpResponse response = httpclient.execute(httpget, localContext);
HttpHost target = (HttpHost) localContext.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);// this is the final page of the request
System.out.println("Final target: " + target);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
Thanks
If your provider is lying to you by immediately returning a 200 OK but not giving you the resource you've requested, your best option is probably to set a custom HTTP response header that your client can check before continuing.
I am suppose to send song (mp3/wav) file and some data through secure restful web service. I am using MultipartEntity to make HttpPost request. but When I execute it through HttpClient, the server replies this error
HTTP Status 400 - Bad Request
type: Status report
message : Bad Request
The request sent by the client was syntactically incorrect (Bad Request).
But the service is doing very well if we call it from its Web interface. please help
its the code
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost();
try {
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("email", new StringBody("test#testmail.com"));
reqEntity.addPart("password", new StringBody("123"));
reqEntity.addPart("title", new StringBody("My new song"));
reqEntity.addPart("musicData", new FileBody(new File(FilePath)));
// FIlePath is path to file and contains correct file location
postRequest.setEntity(reqEntity);
postRequest.setURI(new URI(ServiceURL));
HttpResponse response = httpclient.execute(postRequest);
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
I also included apache-mime4j, httpclient, httpcore and httpmime jars for MultipartEntity.
This is HTML page snap for the Service.
Try removing the setURI method and passing the URL in when you create your HttpPost object, as follows. This worked for me (more here).
HttpClient httpclient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(ServiceURL);
try {
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("email", new StringBody("test#testmail.com"));
reqEntity.addPart("password", new StringBody("123"));
reqEntity.addPart("title", new StringBody("My new song"));
reqEntity.addPart("musicData", new FileBody(new File(FilePath)));
postRequest.setEntity(reqEntity);
HttpResponse response = httpclient.execute(postRequest);
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
It seems header of the request is incorrect, this problem can occur if you use a different Auth protocol or upper/lower case or simply wrong things in header that server side can't handle.
Dont waste your time by trying different different combinations.There are some HTTP Request tools available for HTTP with which you can track request and response you are getting.Ex. HTTP Analyzer download trial version
Call URL from your working webinterface , copy request and response
then do same with from program the tool is enogh capable to capture your request and response data.
Now compare working and non working request you will surely able to dignose the issue whether it can be header issue or some authentication related issue.