Sending post request from a servlet - java

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?"

Related

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

How to create authorization URL request with paramters

im trying to work with yahoo Gemini api
which need first to implement using Ouath 2.0
going into this link
Its saying i need to create a request to a URL with "Request Parameters"
client_id
redirect_uri
now lets say i do it in java:
this is my HTTP request:
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new
HttpPost("https://api.login.yahoo.com/oauth2/request_auth");
is this how i added paramters to the request ?
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("client_id", "ABCDEFGH"));
params.add(new BasicNameValuePair("redirect_uri", "http://www.goTo.Com"));
is this is how i execute the entire request ?
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is this the currect way to Get an authorization URL and authorize access ?
is there any other way / simpler doing that ?
what should i expect in the response ?
i believe that when you're working with Yahoo Gemini, you have to use a specific couple of consumer_key/consumer_secret according to my little investigation as stated in this issue
You check the guide out for an implementation of oauth2 for yahoo apis.
Hope it helped

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

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);

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);

How to set HTTP Request Header "authentication" using HTTPClient?

I want to set the HTTP Request header "Authorization" when sending a POST request to a server.
How do I do it in Java? Does HttpClient have any support for it?
http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z9
The server requires me to set some specific value for the authorization field:
of the form ID:signature which they will then use to authenticate the request.
Thanks
Ajay
Below is the example for setting request headers
HttpPost post = new HttpPost("someurl");
post.addHeader(key1, value1));
post.addHeader(key2, value2));
Here is the code for a Basic Access Authentication:
HttpPost request = new HttpPost("http://example.com/auth");
request.addHeader("Authorization", "Basic ThisIsJustAnExample");
And then just an example of how to execute it:
HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpClient httpclient = null;
httpclient = new DefaultHttpClient(httpParams);
HttpResponse response = httpclient.execute(request);
Log.d("Log------------", "Status Code: " + response.getStatusLine().getStatusCode());
This question is "answered" here:
Http Basic Authentication in Java using HttpClient?
There are many ways to do this. It was frustrating for me to try to find the answer. I found that the best was the Apache docs for HttpClient.
Note: answers will change over time as the libraries used will have deprecated methods.
http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/authentication.html

Categories

Resources