why Http Post working when send to ip but not when send to url? - java

I am using org.apache.http.client.methods.HttpPost to get a line of info from my server.
When I use the IP address of the server (the below is an example only) I get the info as expected:
HttpPost("http://127.0.0.1/api/fetchInfo")
However, when I use the URL the server is not getting a request. The URL includes a subdomain.
HttpPost("http://subdomain.domain.com/api/fetchInfo")
How to make it work with the url?
My code:
String responseString = "";
try {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8")).setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpEntity entity = multipartEntityBuilder.build();
HttpPost request = new HttpPost("http://subdomain.domain.com/api/fetchInfo");
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
responseString = new BasicResponseHandler().handleResponse(response);
} catch (IOException ex) {
log(ex)
}
return responseString;

If you are using the apache server then you need to configure the subdomain.conf file with the server name subdomain.domain.com and in the /etc/hosts file onto the machine you need to specify the mapping of subdomain.domain.com name with the ip 127.0.0.1.
I hope this helps.

Related

Httpclient isn`t working on Android 5.0(Lollipop)

Im trying to send a string with GET, and I have try as well with POST but nothing isn`t working,
this is my code
HttpClient Client = new DefaultHttpClient();
// Create URL string
String URL = "http://10.0.2.2:2711/RequestsHandler.ashx?bulbStatus=off";
//Log.i("httpget", URL);
try {
String SetServerString = "";
// Create Request to server and get response
HttpGet httpget = new HttpGet(URL);
HttpResponse responseGet = Client.execute(httpget);
HttpEntity resEntityGet = responseGet.getEntity();
content.setText(SetServerString);
} catch (Exception ex) {
content.setText("Fail!");
}
I have tried all the local host example 10.0.0.2 10.0.3..... what ever that you can think of
I did all the permissions on the manifest (2)
I have check this on a real server( that works ) nothing happen.
I Think the problem is that the string isn`t send to the server ( I have check it its the problem)
please someone can help me ?

Java DefaultHttpClient HTTP PUT and Cookie

I'm send a request to YouTrack api to create issue.
String url = yBaseUrl + "/rest/issue?Task&"+ URLEncoder.encode(subject)+"&"+URLEncoder.encode(desc);
HttpClient client = new DefaultHttpClient();
HttpPut request = new HttpPut(url);
// add request header
((DefaultHttpClient) client).setCookieStore(cookie);
HttpResponse response = null;
//client.execute(post);
try {
response = client.execute(request);
System.out.println(response.getStatusLine().getStatusCode());
} catch (IOException e) {
e.printStackTrace();
}
result - 403 code.
Why setCookieStore not working?
The problem was incorrect api url. Need use /rest/issue?project=Task&summary="+ URLEncoder.encode(subject)... The question is closed

Could not find HttpResponse in httpclient 4.3.3

I try to implement a simple http program which send the request and get response from the given URL.For that i use httpclient 4.3.3 jar add as an external library to my project.But the problem is,I could not find the HttpResponse in the jar file.And also i want to handle NTLM authentication in this application.please help me.
Sample code:
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://www.google.com/?q=java");
try {
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine());
} catch (Exception e) {
e.printStackTrace();
}

Android http post advanced request with body on hostmachine

I would like to do a HTTP post request from my virtual android device on the hostmachine.
Below you'll see an image on how I post, by using the old WebFetch tool.
I don't know what URL to use for calling the hostmachine?
I got no idea how my body string can be used an input?
Does anybody have an idea on how to solve this?
If you want to connect to the computer which is running the Android simulator, use the IP address 10.0.2.2. You can read more about it here.
Also check out the accepted answer in following question to see how json can be send as post data:
How to send POST request in JSON using HTTPClient?
you can use following code to make HTTP get request:
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://10.0.2.2:port/your_path_with_parameter";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}

Android error : MultipartEntity , request sent by the client was syntactically incorrect

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.

Resources