I have a webservice which receives 2 (optional) parameters:
<resource path="getLogbookEvents">
<method name="POST">
<request>
<param name="startDate" style="query" type="xs:string"/>
<param name="endDate" style="query" type="xs:string"/>
</request>
<response>
<representation mediaType="application/json"/>
</response>
</method>
</resource>
I'm able to connect and receive an answer from Android using HttpPost, but the web service never receives either of the two parameters.
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://... method URL ...");
//post.setHeader("media-type", "application/json; charset=UTF-8");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("startDate", "2014-04-18T05:00:00"));
nameValuePairs.add(new BasicNameValuePair("endDate", "2014-04-18T06:00:00"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
// entity.setContentEncoding(HTTP.UTF_8);
post.setEntity(entity);
// make POST request to the given URL
HttpResponse httpResponse = httpclient.execute(post);
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
I have tried HttpParams, JSONObject and nameValuePairs and nothing works. I just keep receiving the response as if I didn't specify any parameters. Any ideas on why this could be happening or other things I could try to get the winning combination?
not a direct answer, never used httpclient directly but to debug/fix this issue you could:
use proxy and see what data is really being sent from your phone
use easy to use http libs such as http-request
or retrofit
go for http-request. it is dead simple to use. trust me.
I finally managed to get it working.
So, first of all, there's definitely something wrong with the webservice.
I'm not exactly sure what, and it's not my business to fix it, so I'm not even going to try to pinpoint what it was.
Basically, the getLogbookEvents wasn't receiving parameters because it won't accept any parameters inside the body of the request, period. Looking at the SoapUI raw data being sent I discovered that it was always sending the parameters inside the URL, not the body ...
I have no idea how they got a POST method to behave like a GET method, but as it stands they apparently do. Formatting my nameValuePairs in UTF-8 and adding them to the URL is the (totally backwards and not recommended at all) way I got it to work :)
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("startDate", "2014-04-18T05:00:00"));
nameValuePairs.add(new BasicNameValuePair("endDate", "2014-04-18T06:00:00"));
String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
HttpPost post = new HttpPost("... basic URL String ..." + paramString);
Related
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
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
I am trying to update email alias for different users. I am able to authenticate, get the code and then get the access token. I am sending the access token in the HTTP POST request as a Header. I am using Java & Apache HTTPClient to make the RESTful call. Here is the code snippet (Only relevant code shown).
if (httpClient != null) {
String apiURL = getApiURL();
apiURL = MessageFormat.format(apiURL, "firstname.lastname#company.com");
// apiURL = https://api.box.com/2.0/users/firstname.lastname#company.com/email_aliases
// firstname.lastname#company.com does exist in the Box Account
HttpPost post = new HttpPost(apiURL);
post.addHeader("Authorization", "Bearer "+accessToken);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "updateemail#company.com"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Charset.defaultCharset()));
HttpEntity entity = post.getEntity();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseFromBox = httpClient.execute(post, responseHandler);
writeResponse(response, responseFromBox);
if (responseFromBox != null) {
if (logger.isDebugEnabled()) {
logger.debug("apiURL-->"+apiURL);
logger.debug(responseFromBox);
}
}
}
The problem is that the response I get is some HTML code that says "The page you were viewing has expired. Please go back and try your request again." I was expecting some JSON string.
What I am doing incorrect? In the Post request instead of sending the email address I used the user id. But I get the same error.
In fact when I try to fetch the email alias of a user using the HTTP GET request I get an error "Not Found". The user does exist. I have an admin control. I can see them.
Thanks
Raj
Try a get on /users to get the array of all your users in the enterprise first. Is that working for you? If not, can you do a get on /users/me? If you can't get the former, then your API key may not have the "manage an enterprise" grant setup for it. You have to set that up in the app management, where you setup your OAuth2 callback URL.
Not sure why you are getting HTML back. That usually only happens on badly formed requests that our servers can't even parse, like you are hitting the wrong URL.
Just a reminder, OAuth2 URL is different from the API URL. 1st is https://www.box.com/api/oauth2/.... 2nd is https://api.box.com/2.0/...
As for setting the Email alias, that's entirely possible, once you know the ID of the user you are trying to set the alias for. Documentation is here
I was using the NameValuePair instead of the JSON string that was being expected. So I removed the following
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "updateemail#company.com"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, Charset.defaultCharset()));
and added
String json = "{\"email\":\"firstname.lastname#company.com\"}";
StringEntity entity = new StringEntity(json, Charset.defaultCharset());
post.setEntity(entity);
and then things started to work!
I am trying make an HttpPut Request to the server and send some parameters with it, but however I think that the parameters are not being detected due to which the server send an error message.
My Code is:
URI url = new URI("http://myurl.com/something/something");
HttpClient client = new DefaultHttpClient();
HttpPut hput = new HttpPut(utl);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("id",URLEncoder.encode(ppid,"UTF-8")));
pairs.add(new BasicNameValuePair("name",URLEncoder.encode(netid,"UTF-8")));
hput.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse res = client.execute(hput);
System.out.println(res.getStatusLine);
It says, the PUT method is not supported by the server but the server does support it.
Tried to do a lot research but wasn't successful as most of the posts were just for POST and GET.
Any help would be greatly appreciated.
Thanks.
Most of the servers PUT and DELETE methods are disabled default. Only GET/POST are enabled.
This is roughly the code I'm working with now:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("json", json.toString()));
nameValuePairs.add(new BasicNameValuePair("blob", file.getAbsolutePath()));
post_request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
The the reply I get back from the server is good for the first add() statement but, for the second one I'm not trying to send the path, I'm trying to send the file. Taking off .getAbsolutePath() should do the trick, but It won't let me as it only accepts strings. How would I go about sending the file?
you should use a MultipartEntity, not an UrlEncodedForm one. In a Multipart body you can store objects of different mime types