Edit a value and post to http with HttpPost - java

The problem:I have a few forms in the html page which I want to edit, then submit the data.
I have read about entities in HttpClient, and I came across the UrlEncodedFormEntity, which as far as I understand you add parameters to it and then you can post them. I find this ok, but I thought is there a different way to post the changed attributes, since jsoup has a convenient method to set a value in an attribute. this is what I tried using a different entity, StringEntity:
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", USER_AGENT);
post.setHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
post.setHeader("Accept-Charset", "UTF-8");
post.setHeader("Cookie", getCookies());
post.setHeader("Connection", "keep-alive");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new StringEntity(updatedHTML, ContentType.TEXT_HTML));
HttpResponse response = null;
response = client.execute(post);
where updatedHTML is the full html code with the changes I want to post.
but as you guessed, its not working.
edit: I don't think it's the problem, but I also have a sumbit button, which I ignored here, should it also be considered in the updatedHTML?
Thanks for help.

Two things are wrong in your approach.
you cannot pass an html in the StringEntity as it is not the usage of the class
The StringEntity, as well as its derived classes, is intended to carry messages.
The second error is that you seem to use the library to change the html.
You need to work on what you are posting. Here an example.
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("your parameter name","your parameter value"));
formparams.add(new BasicNameValuePair("another parameter name","another paramete value"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("http://localhost/");
httppost.setEntity(entity);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
I made some assumptions:
you have in your hands all the parameters you are passing (simply you will change the approach not working on the html but on the url)
The exception handling is not considered in my snippet. The code is a simple example to show you how to deal with forms
Note also that the UrlEncodedFormEntity will handle the parameters for you. E.g. in our example>
your parameter name=your parameter value&another parameter name=another parameter value

Related

JAVA - How to make a purchases.products.acknowledge request

I have to make a request with the POST method as described in the guide: https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.products/acknowledge
My Java code currently looks like this:
httpClient = HttpClientBuilder.create().build();
post = new HttpPost("https://androidpublisher.googleapis.com/androidpublisher/v3/applications");
ArrayList<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("packageName", "com.my.app"));
nvps.add(new BasicNameValuePair("productId", productID));
nvps.add(new BasicNameValuePair("token", token));
post.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
response = httpClient.execute(post);
I receive a 404 web page, page not found, what am I wrong with my request?
Thanks to everyone who will try to help me. I love you <3
Without having knowledge about your HTTPClient...
I think you are using the wrong URL.
That is from the docs:
https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/products/{productId}/tokens/{token}:acknowledge
and your URL simply is:
https://androidpublisher.googleapis.com/androidpublisher/v3/applications
Besides that, i think you are posting the params for the URL as an entity in the payload. Instead you have to fill the variables in the url with it.
The only allowed entity in the payload is in the form of:
{
"developerPayload": string
}

POST request body cannot contain chinese characters in Java

I am using Hibernate and GSON to retrieve data as an object in Java, and create a .toString method via GSON in the POJO to make JSON string. Now I need to use this JSON as a request body to send a POST request to a web service.
The problem is that if the JSON contains any chinese character, it will not work. It complains the chinese name field is incorrect. I checked and there are no spaces in the chinese string, and the .length() of that is exactly the number of characters.
I also tried to copy the whole JSON and paste it to Postman to make the POST request. It will work. How come it does not work in my Java code but works in Postman? Below is how I create the POST request.
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(CONNECTION_TIMEOUT)
.setConnectTimeout(CONNECTION_TIMEOUT)
.setSocketTimeout(CONNECTION_TIMEOUT)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
StringBuilder entity = new StringBuilder();
entity.append(myJsonFromPojo);
HttpPost post = new HttpPost(uri);
post.addHeader("content-type", "application/json");
post.addHeader("Content-Type", "charset=UTF-8");
post.addHeader("Accept", "*/*");
post.addHeader("Accept-Encoding", "gzip,deflate");
post.addHeader("Authorization", "Bearer " + token);
post.setEntity(new StringEntity(entity.toString()));
response = httpClient.execute(post);
Manage to use below to solve my issue. post.addHeader("Content-type", "application/json; charset=UTF-8"); post.addHeader("Accept", "application/json");

How to send POST request using HTTPURLConnection with JSON data

I have a RESTful API that I can call by doing the following:
curl -H "Content-Type: application/json" -d '{"url":"http://www.example.com"}' http://www.example.com/post
In Java, when I print out the received request data from the cURL, I correctly get the following data:
Log: Data grabbed for POST data: {"url":"http://www.example.com/url"}
But when I send a POST request via Java using HttpClient/HttpPost, I am getting poorly formatted data that does not allow me to grab the key-value from the JSON.
Log: Data grabbed for POST data: url=http%3A%2F%2Fwww.example.com%2Furl
In Java, I am doing this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/post/");
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
BasicNameValuePair nvp1 = new BasicNameValuePair("url", "http://www.example.com/url);
nameValuePairs.add(nvp1);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpresponse = httpclient.execute(httppost);
How do I make it so that the request from Java is similar to cURL in terms of how the data is sent?
The data you present as coming from the Java client are URL-encoded. You appear to specifically request that by using a UrlEncodedFormEntity. It is not essential for the body of a POST request to be URL-encoded, so if you don't want that then use a more appropriate HttpEntity implementation.
In fact, if you want to convert generic name/value pairs to a JSON-format request body, as it seems you do, then you probably need either to use a JSON-specific HttpEntity implementation or to use a plainer implementation that allows you to format the body directly.

POSTing arraylist

in most of the q&a its Name Value Pair that is used to POST;
List<NameValuePair> myBooks = new ArrayList<NameValuePair>();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(myBooks));
but when I try to change the params to to arrayList,
List<Books> myBooks= //fulfilled from another class"
I get this:
The constructor UrlEncodedFormEntity(List<myBooks>) is undefined
is it really impossible to post List instead of ValuePair without Jackson lib?
If you look at the API you can see that the list must must extend NameValuePair.
So your own class would need to extend the NameValuePair class as well.
But why would this be? If you look at the third line of the code you provided
httpPost.setEntity(new UrlEncodedFormEntity(myBooks));
The important part here is noticing the UrlEncodedFormEntity.
The UrlEncoded data would consist of simple name & value pairs, like this:
name=bob&age=20
If you provide a list that already consists of names with their values(NameValuePair), you get this done for you, by the library.
If you want to just POST arbitary data, you can do this by yourself, by using StringEntity.
httpPost.setEntity(new StringEntity(myString));
In any case, please do not forget to add the revelant content-type header to your POST!

UrlEncodedFormEntity doesn't encode underscore

I want to use a remote API from my Android device, but for some reason, the UrlEncodedFormEntity class doesn't transform the _ with %5f like the remote API seems to expect. As a consequence, using this code:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(
new BasicNameValuePair("json",
"{\"params\":{\"player_name\":\"Toto\",
\"password\":\"clearPass\"},
\"class_name\":\"ApiMasterAuthentication\",
\"method_name\":\"login\"}")
);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
ResponseHandler responseHandler = new BasicResponseHandler();
httpClient.execute(httpPost, responseHandler);
send a post request to the server, with this content:
json=%7B%22params%22%3A%7B%22player_name%22%3A%22Toto%22%2C%22password%22%3A%22clearPass%22%7D%2C%22class_name%22%3A%22ApiMasterAuthentication%22%2C%22method_name%22%3A%22login%22%7D
I would like it to be like this (replacing the preivous underscore by %5F):
json=%7B%22params%22%3A%7B%22player%5Fname%22%3A%22Toto%22%2C%22password%22%3A%22clearPass%22%7D%2C%22class%5Fname%22%3A%22ApiMasterAuthentication%22%2C%22method%5Fname%22%3A%22login%22%7D
I don't have control over the API, and the official client of the API behave like this. It seems to be the expected behaviour for an URL normalization
Am I missing something? I first thought it was an UTF-8 encoding issue, but adding HTTP.UTF-8 in the constructor of UrlEncodedFormEntity doesn't solve the problem.
Thanks for your help.
EDIT: Finally, the problem didn't come from this unescape underscore. Even if the other client I tried to reproduce the behaviour escaped it, I only had to set the proper header:
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
And the request worked just fine. Thanks everyone, and especially singh.jagmohan for his help (even if the problem was finally elsewhere)!
"_" isn't a reserved symbol for urls.
setting : Content-Type: application/x-www-form-urlencoded'
should solve the problem. Otherwise you can try replacing it, if you really need this option:
String.Replace("_", "%5f");
See percent encodeing , replace
You can try the following code, it works for me.
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
MultipartEntity multipartEntity = new MultipartEntity();
// Also, in place of building JSON string as below, you can build a **JSONObject**
// and then use jsonObject.toString() while building the **StringBody** object
String requestJsonStr = "{\"params\":{\"player_name\":\"Toto\",\"password\":\"clearPass\"},\"class_name\":\"ApiMasterAuthentication\",\"method_name\":\"login\"}";
multipartEntity.addPart("json", new StringBody(requestJsonStr));
httpPost.setEntity(multipartEntity);
HttpResponse response = httpClient.execute(httpPost);
} catch (Exception ex) {
// add specific exception catch block above
// I have used this one just for code snippet
}
PS: The code snippet requires two jar files apache-mime4j-0.6.jar and httpmime-4.0.1.jar.
Hope this helps.

Categories