Send UrlEncodedForm data using euc-jp in Apache HttpClient - java

I am wondering how I can send form data using euc-jp encoding. My attempt at encoding below is still sending japanese text as ? and odd characters. Thank you!
This is how I am currently doing it (not working properly):
HttpPost request = new HttpPost("http://httpbin.org/post");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Testing", "雄大"));
request.setEntity(new UrlEncodedFormEntity(params, forName("EUC-JP")));

Your code seems good to me. httpbin.org doesn't seem to be handle EUC-JP in response. Instead you can use putsreq.com to see your request parameters.
import java.util.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.methods.*;
import org.apache.http.NameValuePair;
import java.nio.charset.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.*;
import org.apache.http.*;
import java.io.*;
class Main {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
// Create new PutsReq URL by yourself
HttpPost request = new HttpPost("https://putsreq.com/xxxxxxxxxxxxxxxxxxxx");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Testing", "雄大"));
request.setEntity(new UrlEncodedFormEntity(params, Charset.forName("euc-jp")));
HttpResponse response = httpclient.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
while ((reader.readLine()) != null) {
System.out.println (reader.readLine());
}
reader.close();
}
}
And you will see
Testing=%CD%BA%C2%E7
in the inspect page. 0xCDBA means 雄 in EUC-JP.

Related

Apache HttpClient post request with non string values in body

I'm using Apache HttpClient version 4.5.13 and I'm having trouble creating my POST request body. From the tutorials I've seen online, they tell me to use NameValuePair when creating the request entity. However, NameValuePair only accepts String for the values.
How can I set Integer, Double, and Booleans as well? The API I'm calling has a mixture of them in the JSON body. For example, my body can look like this:
{
"id": 3,
"name": "Test",
"accepted": false,
"street": "foo bar ave."
}
This is how the documentations recommend I create the body:
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public static void main(String[] args) {
List<NameValuePair> body = new ArrayList<NameValuePair>();
body.add(new BasicNameValuePair("id", Integer.valueOf(3)));
body.add(new BasicNameValuePair("name", "Test"));
body.add(new BasicNameValuePair("accepted", Boolean.valueOf(false)));
body.add(new BasicNameValuePair("street", "foo bar ave."));
HttpPost httpPost = new HttpPost("http://my-url.com/test-api");
httpPost.setEntity(new UrlEncodedFormEntity(body));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);
// etc...
}
But as you can see, I have to convert everything to String which the API I'm calling rejects because of the mismatch of data types. Is there some other way I can create the request which accepts various data types?
I was able to fix this by using StringEntity. I converted the code in the question to the following:
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", 3);
jsonObject.put("name", "Test");
jsonObject.put("accepted", false);
jsonObject.put("street", "foo bar ave.");
HttpPost httpPost = new HttpPost("http://my-url.com/test-api");
httpPost.setEntity(new StringEntity(jsonObject.toString()));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);
// etc...
}
And I was able to call the API in question without any errors. Hopefully, this helps someone!

Able to hit in postman but not in java

I am not able to get a response from eclipse when integrated in java code. I am able to retrieve the response from postman/insomnia, but not from eclipse. I masked the token and the URL in this image.
My current code is:
public class Test{
public static void main(String[] args) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
httppost.addHeader("Authorization", "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
httppost.addHeader("Accept", "*/*");
httppost.addHeader("Content-type", "multipart/form-data; boundary=X-INSOMNIA-BOUNDARY");
httppost.addHeader("Host","process-workorders-mti64mke4a-uc.a.run.app");
File fileToUse = new File("D:\\firstImage.jpg"); // this is the image I am uoploadin
FileBody data = new FileBody(fileToUse);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("mode", new StringBody("api"));
reqEntity.addPart("file", data);
*// seems there is issue here in passing form parameters*
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
System.out.println( response ) ;
HttpEntity resEntity = response.getEntity();
System.out.println( resEntity ) ;
System.out.println( EntityUtils.toString(resEntity) );
EntityUtils.consume(resEntity);
httpclient.getConnectionManager().shutdown();
}
}
Below are the imports:
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
Tried OKHTTPClient and it worked

How to avoid URIBuilder to encode comma to "%2C" or other characters in query params

I would like to create a URI using apache class org.apache.http.client.utils.URIBuilder and I need to not encode query params to percent-encoding.
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.net.URI;
public class Main {
public static void main(String[] args) throws Exception {
RequestConfig config = RequestConfig.custom().build();
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(config);
// URI url = new URI("http://some-website.com/?range=10,20");
// If url is created with the line above the comma "," is not encoded when sending the request
// When you use URIBuilder the comma "," is converted to "%2C"
URIBuilder uribuilder = new URIBuilder("http://some-website.com/");
uribuilder.addParameter("range", "10,20");
URI url = uribuilder.build();
System.out.println("URL => " + url.toString());
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getScheme());
HttpClient client = builder.build();
HttpRequestBase req = new HttpPost(url);
HttpResponse httpResponse = client.execute(targetHost, req);
HttpEntity entity = httpResponse.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseString);
System.out.println("Finished");
}
}
Is there a way to do that using the URIBuilder class like some flag I don't know?
I would appreciate other suggests (maybe better ways than my code) to accomplish this. But I can't send the characters in query string encoded.
Thanks in advance.

Post request to login in Booking.com from Java

I need to login in Booking.com and i am trying to do it in Java. I made a post request in different ways but i can not get the html from de index page.
This is the target page:
Admin Booking
I need the following parameters for login:
loginname=
password=
ses=
lang=en
login=Login
I know that these are the parameters because a partner made that login in python and it works.
The ses parameter is on the form login (as a hidden input field) and the loginname and password are provided by myself.
So... to get the ses i make a previous GET request and then i add it as a parameter in my POST request. I get without problems the html from the first request but not the second one (POST).
I know that the POST login resquest should send the html of the logged page because as i said above a partner of mine obtains that result in python. In addition, i also tried this with Postman Chrome Application (Postman) and it works fine (with the difference that i only provide loginname and password).
Here is my code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Test {
private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "https://admin.booking.com/hotel/hoteladmin/login.html";
private static final String POST_URL = "https://admin.booking.com/hotel/hoteladmin/login.html";
public static void main(String[] args) throws IOException {
String ses = sendGET();
System.out.println("GET DONE");
sendPOST(ses);
System.out.println("POST DONE");
}
private static String sendGET() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(GET_URL);
httpGet.addHeader("User-Agent", USER_AGENT);
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
System.out.println("GET Response Status:: "
+ httpResponse.getStatusLine().getStatusCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();
// print result
Document doc = Jsoup.parse(response.toString());
String ses = doc.select("#ses").val();
System.out.println(response.toString());
httpClient.close();
return ses;
}
private static void sendPOST(String ses) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(POST_URL);
httpPost.addHeader("User-Agent", USER_AGENT);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("loginname", "467933"));
urlParameters.add(new BasicNameValuePair("password", "moncloa1895"));
urlParameters.add(new BasicNameValuePair("ses", ses));
urlParameters.add(new BasicNameValuePair("lang", "en"));
urlParameters.add(new BasicNameValuePair("login", "Login"));
HttpEntity postParams = new UrlEncodedFormEntity(urlParameters);
httpPost.setEntity(postParams);
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println("POST Response Status:: "
+ httpResponse.getStatusLine().getStatusCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();
// print result
System.out.println(response.toString());
httpClient.close();
}
}
Anyone knows how can i solve it??
Thanks!
You should follow redirects. Add something like this:
httpPost.setRedirectStrategy(new LaxRedirectStrategy());
Im not sure what the actual method is on HTTPPost, but HttpClient has this ability.

Trying to download data from a password protected website

I have a URL with data I would like to access. The data is in text form and it is password protected...but here's the thing...it is password protected by a login on a different website. Every time I try to gain access to the data URL, I get an HTTP 500 error. Any suggestions for this issue? I don't think this is a very common problem considering I have not come across it in my many Stackoverflow and Google searches.
Below is an example of one of the programs I have tried using to no avail...(Some of the information is private, so I changed the username, password, and url)
package Apache1;
//import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.net.*;
import java.io.*;
/**
* A simple example that uses HttpClient to execute an HTTP request against
* a target site that requires user authentication.
*/
public class Apache2 {
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 443),
new UsernamePasswordCredentials("myUsername", "myPASSWORD"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("LOGIN_WEBSITE");
System.out.println("Executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(response.getEntity());
URL oracle = new URL("DATA_WEBSITE");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
}

Categories