Sending HTTP command in browser works, not when sent with httppost - java

When in my browser I send the following string to a control unit I have http://192.168.0.215/i_activate/aterm?40~00 and a relay is activated.
I have tried many variations of the following:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.215/i_activate/aterm?40~00");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
With an HTML response "FAIL" from the unit
I have tried adding the 40~00 in many ways (NameValuePair, etc) and encoded in different forms without success but I am sure the problem lies there.
Any thoughts?

The problem is that the browser sends a GET request, where the parameter is in the URL itself as a query string, but you are sending a POST request without any body data.
Use HttpGet instead of HttpPost to send a GET request:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://192.168.0.215/i_activate/aterm?40~00");
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpget);

Related

While trying to send POST request in HTTPCLIENT -JAVA, getting 400 Bad Request

I am trying to POST a request using JAVA HTTPCLIENT, and while doing so, I am getting 404 Bad Request.
I tried writing the JAVA code in Eclipse and got 404 Bad Request and tried sending the request through POSTMAN and received HTTP Status 500
package com.apex.customer.service;
public class CustServicePostTest {
public static void main(String[] args) throws ClientProtocolException, IOException {
String url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/102";
//create the http client
HttpClient client = HttpClientBuilder.create().build();
//create the post message
HttpPost post = new HttpPost(url);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("ID", "102"));
urlParameters.add(new BasicNameValuePair("FIRSTNAME", "Apex"));
urlParameters.add(new BasicNameValuePair("LASTNAME", "Consultancy"));
urlParameters.add(new BasicNameValuePair("STREET", "Shell Blvd"));
urlParameters.add(new BasicNameValuePair("CITY", "Fremont"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println("Parameters : " + urlParameters);
System.out.println("Response Code: " + response);
System.out.println(response.getStatusLine().getReasonPhrase());
}
}
I am looking for 200 OK request.
The issue here is due few mistakes:
First is related to the input format. The code you're using tries to map key and values, but as I could see from this guide, it expects a XML format in a plain text as input.
The second mistake is that you are trying to post over an existing ID. In this case, to create a resource you should use http://www.thomas-bayer.com/sqlrest/CUSTOMER/
So in this case in order to make it work, try something like this:
String url = "http://www.thomas-bayer.com/sqlrest/CUSTOMER/";
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String xml = "<resource>";
xml += "<ID>102</ID>";
xml += "<FIRSTNAME>Apex</FIRSTNAME>";
xml += "<LASTNAME>Consultancy</LASTNAME>";
xml += "<STREET>Shell Blvd</STREET>";
xml += "<CITY>Fremont</CITY>";
xml += "</resource>";
post.setEntity(new StringEntity(xml));
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
System.out.println("Response Code: " + response);
System.out.println(response.getStatusLine().getReasonPhrase());
It is also very useful to learn another way to test it with tools like curl command line utility. For example you can POST a product like this:
curl -X POST http://www.thomas-bayer.com/sqlrest/PRODUCT/ -d '<resource><ID>103</ID><NAME>X</NAME><PRICE>2.2</PRICE></resource>'
Once you solve this, it will be important to get used with HTTP codes. For example a 500 error means something wrong on the server side while a 404 usually means that you're hitting an invalid endpoint (it does not exists).
Finally, I'll not discuss why are you using this project to send HTTP requests to a server - but keep in mind that this is not a very common way to go. Currently the REST with JSON would be much more interesting and enjoyable :) In case you're interested on it, take a look on Spring Boot REST

Correct way to call telegram api methods

I'm working on Telegram api in my java application. I need to do authentication and authorization with my telegram account and get message list of my specific group. For this purpose, first I got api_id, api_hash and MTProto servers from telegram site. Second, I tried to authorize my account with auth.sendCode method in this way:
...
String url = "https://149.154.167.40:443/auth.sendCode";
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.addHeader("charset", "UTF-8");
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("phone_number", myPhoneNumber));
nameValuePairs.add(new BasicNameValuePair("sms_type", "5"));
nameValuePairs.add(new BasicNameValuePair("api_id", api_id));
nameValuePairs.add(new BasicNameValuePair("api_hash", api_hash));
nameValuePairs.add(new BasicNameValuePair("lang_code", "en"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
...
But this returns me javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake exception. I tested url with http instead of https and this returned 404 Not Found html content. What is the correct way for calling telegram api method in java?
Update:
I tried using java socket for sending TCP post request, but this returns me 404 not found.
Since it's mproto protocol, you must obey their specification - https://core.telegram.org/mtproto
I suggest you to use this project, since it has working examples - https://github.com/badoualy/kotlogram

HttpClient , httppost and payload

I'm trying to use apache httpclient.
I have a httppost query.
To build my object I use the info provided by chrome developer tool.
When I connect do the server I see a http post request and info under payload:
7|0|10|http://sfe.corpe15.com:8980/etrading/etrading/|0A6AA27FE80C74E3200C24AA537362BD|com.gwtplatform.dispatch.shared.DispatchService|execute|
How does that translate in Java? How do I send the payload in my httppost object?
Thanks!
I figured it out! It looks like this!
String payLoadLogin = "blahblahblah"
HttpPost httppost = new HttpPost(postAddress);
httppost.setHeader("X-GWT-Permutation", "3DE824138FE65400740EC1816A73CACC");
httppost.setHeader("Content-Type", "text/x-gwt-rpc; charset=UTF-8");
StringEntity se = new StringEntity(payLoadLogin );
httppost.setEntity(se);

What do I need to set up for the execute() of org.apache.http.client.HttpClient to work?

I am new to android and I'm making a simple app that sends a JSON object to the server. The many examples that I found on the internet had the following three lines of code:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(prepai.host22.com/LoadStory.php);
HttpResponse httpResponse = httpClient.execute(httpPost);
I tried to run them but the app running in the eclipse emulator crashed when the execute() function got executed. I got this log:
Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=prepai.host22.com/LoadStory.php
How do I give the host a value? Also do I have to do all the communication in a separate thread? Or is there anything else that I need to do?
You should pass the full url (including the http or https) as a string, for example:
HttpPost httpPost = new HttpPost("http://prepai.host22.com/LoadStory.php");

Sending post request from a servlet

I have been trying to send a POST request from a servlet and the code that I wrote is this:
HttpPost post = new HttpPost(url.build());
post.setEntity(new UrlEncodedFormEntity(nameValue));
post.setHeader("content-type", "application/json");
HttpClient client = new DefaultHttpClient();
HttpResponse res = client.execute(post);
The URL is "https://accounts.google.com/o/oauth2/token" and I send some parameters with this request.
However, when I run this I get AccessControlException.
The HttpPost method that I use here comes from org.apache.http i.e., commons HttpClient API. I have tried a lot but it doesn't seem to work.
So the question is "Is it possible to send a cross domain request using HttpClient?"

Categories

Resources