Encoding problem using Apache HTTP client to download Chinese website - java

I have an encoding issue downloading a Chinese RSS url http://finance.qq.com/stock/ggjj/rss_ggjj.xml
import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class Test {
public static void main(String[] args) {
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
String url = "http://finance.qq.com/stock/ggjj/rss_ggjj.xml";
response = httpclient.execute(new HttpGet(url));
System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The output contains bad encoded characters:
... �ㄨ����搴�锛�涓��ㄤ�娉煎��������姘� ...

Related

How to set up a multipart POST request in java 11?

I am trying to build a multipart HttpRequest using Java 11 as below. I am also trying to pass username
and password and later i might need a file also in the same request. However i keep getting 415 or 400 bad request errors.
The code is below.
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Client{
private HttpClient httpClient;
private HttpResponse httpResponse;
private String Response;
private Map<Object, Object> urlParameters;
public Client(String URL) {
httpClient=BuildClient(URL);
urlParameters= new HashMap<>();
urlParameters.put("username","xxxx");
urlParameters.put("password","xxxx");
try {
PostRequest(httpClient,URL,urlParameters);
} catch (IOException e) {
System.out.println("Client Error");
e.printStackTrace();
}
}
public HttpClient BuildClient(String URL) {
return HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(10))
.build();
}
//This is the code for request build
public HttpResponse PostRequest(HttpClient httpClient, String url, Map<Object, Object> params) throws IOException {
String boundary = new BigInteger(256, new Random()).toString();
HttpRequest request = HttpRequest.newBuilder()
//.POST(HttpRequest.BodyPublishers.fromPublisher(subscriber -> ))
.POST(HttpRequest.BodyPublishers.noBody())
.POST(HttpRequest.BodyPublishers.ofString("{\"username\":\"XXXXX\"}{\"password\":\"XXXX\"}"))
//.POST(HttpRequest.BodyPublishers.ofFile(Path.of("")))
.uri(URI.create("http://example.com/request/add"))
.setHeader("User-Agent", "firefox")
.setHeader("Content-Type", "multipart/form-data")
.build();
HttpResponse<String> response = null;
try {
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(request.headers());
// print status code
System.out.println(response.statusCode());
// print response body
System.out.println(response.body());
return response;
}
public HttpRequest.BodyPublisher FormatData(Map<Object, Object> data) {
var builder = new StringBuilder();
for (Map.Entry<Object, Object> entry : data.entrySet()) {
if(builder.length()>0)
{
builder.append("&");
}
builder.append(URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8));
builder.append("=");
builder.append(URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8));
}
return HttpRequest.BodyPublishers.ofString(builder.toString());
//return null;
}
}
I was wandering if can be done without add some library as maven dependency such as apache HTTP client.

How can I upload pdf files in Egnyte using Rest API and Java

Having the PDF files in local drive(D:). Need to upload those PDF files in Egnyte with the help of Rest API and Java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
#SuppressWarnings("deprecation")
public class Postrequest {
public static void postRequest(String attachmentValue) throws Exception {
String attachment = "C:\\Users\\eclipse-workspace\\renamedfile\\"+attachmentValue;
String url = "https://domain.egnyte.com/pubapi/v1/fs-content/Shared/a/Sa/Upload/"+attachmentValue;
File inFile = new File(attachment);
FileInputStream fis = null;
FileBody data = new FileBody(inFile);
try {
fis = new FileInputStream(inFile);
HttpClient httpclient = HttpClientBuilder.create().build();
// server back-end URL
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Authorization", "Bearer " + "*****");
httppost.addHeader("Content-Disposition", "form-data; name=\"file\"");
//httppost.addHeader("Content-type", "****");
httppost.addHeader("Content-type", "text/plain");
httppost.addHeader("Host", "domain.egnyte.com");
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("document", data);
HttpEntity httpEntity = entity.build();
httppost.setEntity(httpEntity);
// execute the request
HttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
Assert.assertEquals(statusCode, 200,"The Status code is not matched");
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
System.out.println("[" + statusCode + "] " + responseString);
} catch (ClientProtocolException e) {
System.err.println("Unable to make connection");
} catch (IOException e) {
System.err.println("Unable to read file");
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
}
}
};

Fetch Appdynamics data in Java

I want to execute the below command through Java code. This is to create connection to my Appdynamics Contoller
curl --user user#customer1:password 'http://192.168.1.11:9090/controller/rest/applications?output=JSON'
The java code that I have written for this is
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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;
public class Test {
private static HttpClient client;
public static void main(String[] args) {
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = null;
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("192.168.1.11", 9090), new UsernamePasswordCredentials(
"user", "password"));
httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
HttpPost httpget = new HttpPost("http://192.168.1.11:9090/controller/rest/applications?output=JSON");
System.out.println("Executing request " + httpget.getRequestLine());
response = httpclient.execute(httpget);
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
I keep on getting below error
HTTP/1.1 401 Unauthorized
Error report HTTP Status 401 - type Status reportmessagedescriptionThis request requires HTTP authentication ().
Can anyone please help.

Apache HttpClient get add a byte range header?

Does anyone know of how to request byte ranges along with an HTTP request? I am looking to facilitate the resuming of downloads in our application by requesting a byte range of where the download left off and reading its InputStream from getContent().
I tried iterating over the headers but they are null. Source is below.
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
/**
* #author Kevin Kowalewski
*
*/
public class DownloadClient {
DefaultHttpClient httpClient;
HttpGet httpGet;
HttpResponse httpResponse;
HttpEntity httpEntity;
InputStream httpInputStream;
private static String LOG_TAG = DownloadClient.class.getName();
public DownloadClient(){
httpClient = new DefaultHttpClient();
httpGet = new HttpGet("http://cachefly.cachefly.net/100mb.test");
for (Header header : httpGet.getAllHeaders()){
Log.d(LOG_TAG, "--> Header: " + header);
}
Log.d(LOG_TAG, "--> Header size is: " + httpGet.getAllHeaders().length);
try {
httpResponse = httpClient.execute(httpGet);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpEntity = httpResponse.getEntity();
try {
httpInputStream = httpEntity.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(LOG_TAG, "--> StatusLine: " + httpResponse.getStatusLine());
}
public void read(){
byte[] readBuffer = new byte[32];
try {
while (httpInputStream.read(readBuffer) != -1){
try{Thread.sleep(100);}catch(Exception e){}
//Log.d(LOG_TAG,"--> Read Bytes: " + new String(readBuffer));
};
} catch (IOException e) {
e.printStackTrace();
}
}
public void shutdown(){
httpGet.abort();
httpClient.getConnectionManager().shutdown();
}
}
Kevin
I was able to get this working by adding the following line:
httpGet.addHeader("Range", "bytes=0-0");

HTTPclient POST with problematic web site

I'm trying to retrive some data from a web site.
I wrote a java class which seems to work pretty fine with many sites but it doesn't work with this particular site, which use extensive javascript in the input fomr.
As you can see from the code I specified the input fields taking the name from the HTML source, but maybe this website doesn't accept POST request of this kind?
How can I simulate an user-interaction to retrieve the generated HTML?
package com.transport.urlRetriver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class UrlRetriver {
String stationPoller (String url, ArrayList<NameValuePair> params) {
HttpPost postRequest;
HttpResponse response;
HttpEntity entity;
String result = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
postRequest = new HttpPost(url);
postRequest.setEntity((HttpEntity) new UrlEncodedFormEntity(params));
response = httpClient.execute(postRequest);
entity = response.getEntity();
if(entity != null){
InputStream inputStream = entity.getContent();
result = convertStreamToString(inputStream);
}
} catch (Exception e) {
result = "We had a problem";
} finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}
void ATMtravelPoller () {
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(2);
String url = "http://www.atm-mi.it/it/Pagine/default.aspx";
params.add(new BasicNameValuePair("ctl00$SPWebPartManager1$g_afa5adbb_5b60_4e50_8da2_212a1d36e49c$txt_address_s", "Viale romagna 1"));
params.add(new BasicNameValuePair("ctl00$SPWebPartManager1$g_afa5adbb_5b60_4e50_8da2_212a1d36e49c$txt_address_e", "Viale Toscana 20"));
params.add(new BasicNameValuePair("sf_method", "POST"));
String result = stationPoller(url, params);
saveToFile(result, "/home/rachele/Documents/atm/out4.html");
}
static void saveToFile(String toFile, String pos){
try{
// Create file
FileWriter fstream = new FileWriter(pos);
BufferedWriter out = new BufferedWriter(fstream);
out.write(toFile);
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return stringBuilder.toString();
}
}
At my point of view, there could be javascript generated field with dynamic value for preventing automated code to crawl the site. Send concrete site you want to download.

Categories

Resources