0

Using HttpEntity I am getting a very long text which has special characters as well along with Json.

I tried regex but it's not working as it is almost 30000 of characters.

Is there a way that i can only get Json data from the HttpEntity. Even string split did not work since it has so many of special characters.

public JSONObject sendGet(String URL, String userName, String password) throws Exception {
    getRequest = new HttpGet(URL);
    getRequest.addHeader("User-Agent", USER_AGENT);
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
    provider.setCredentials(AuthScope.ANY, credentials);
    client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
    response = client.execute(getRequest);
    HttpEntity entity = response.getEntity();
    outputFile = new File(directoryPath + "/target/response.txt");
    fos = new FileOutputStream(outputFile);
    headers = response.getAllHeaders();
    bw = new BufferedWriter(new OutputStreamWriter(fos));
    for (Header header: headers) {
        bw.write(header.getName() + ": " + header.getValue() + "\n");
    }
    bw.write(response.getEntity());
    bw.write("Response Code : " + response.getStatusLine());
    String content = EntityUtils.toString(entity); //When i print content it has string other than json as well
    JSONObject obj = new JSONObject(content); //Here i receive A JSONObject text must begin with '{' at 1 [character 2 line 1]
    JSONArray keys = obj.names();
    Object test = JSON.parse(content);
    jsonFiles = new File(directoryPath + "/JsonFiles/test.json");
    fos = new FileOutputStream(jsonFiles);
    bw = new BufferedWriter(new OutputStreamWriter(fos));
    bw.write(content);
    bw.close();
    return obj;
}
payne
  • 4,691
  • 8
  • 37
  • 85
mickyslate
  • 11
  • 3
  • Can you share your code which you have written? – Raj Sep 04 '18 at 07:08
  • You need to encode (BASE64) data first before sending if possible. It will preserve ASCII. – Oomph Fortuity Sep 04 '18 at 07:51
  • *//When i print content it has string other than json as well* paste an example. BTW why are you getting SOMETHING + JSON actually insteed of plain json? – Antoniossss Sep 04 '18 at 08:10
  • How to get plain Json, that's what i want. – mickyslate Sep 04 '18 at 08:35
  • How can we answer if we dont know how actual response look like tell me please. – Antoniossss Sep 04 '18 at 08:40
  • I am able to resolve it, i just wrote down the content to json file and only Json data is showing there. However i am not sure why its prints other random message with json whenever i tried to parse the content to Json object directly in code. – mickyslate Sep 04 '18 at 08:50

1 Answers1

1

Try adding the following Headers:

getRequest.addHeader("Accept", "application/json");
getRequest.addHeader("Content-Type", "application/json");
getRequest.addHeader("Accept-Charset", "utf-8");
payne
  • 4,691
  • 8
  • 37
  • 85