DefaultHttpClient is deprecated in Scala

2.1k Views Asked by At

I am trying to hit an external API (authentication required) with LD-Json data from another URL. Not getting how to add key and password for that external API. Tried to use DefaultHttpClient, but it's deprecated.

First approach-

import java.nio.charset.StandardCharsets
import java.util.Base64

import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials}
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{BasicCredentialsProvider, DefaultHttpClient, HttpClientBuilder}
import org.jsoup.Jsoup

class Scraper(url: String) {
  def getJson(url: String) = {
    val doc = Jsoup.parse(url)
    val api_url = "external_api"
    val username = "my_username"
    val password = "my_password"
    val ldJsons = doc.select("script[type=\"application/ld+json\"]")
    val base64EncodedDoc = Base64.getEncoder.encodeToString(doc.toString().getBytes(StandardCharsets.UTF_8))

    val post = new HttpPost(api_url)
    post.setHeader("Content-type", "application/json")
    post.setEntity(new StringEntity(base64EncodedDoc))
    val response = (new DefaultHttpClient).execute(post)
//need an alternative for this in Scala
  }
}

Second approach-

import java.nio.charset.StandardCharsets
import java.util.Base64

import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials}
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{BasicCredentialsProvider, DefaultHttpClient, HttpClientBuilder}
import org.jsoup.Jsoup

class Scraper(url: String) {
  def getJson(url: String) = {
    val doc = Jsoup.parse(url)
    val api_url = "external_api"
    val username = "my_username"
    val password = "my_password"
    val ldJsons = doc.select("script[type=\"application/ld+json\"]")
    val base64EncodedDoc = Base64.getEncoder.encodeToString(doc.toString().getBytes(StandardCharsets.UTF_8))
    val credentialsProvider = new BasicCredentialsProvider()
    credentialsProvider.setCredentials(
      AuthScope.ANY,
      new UsernamePasswordCredentials(username, password)
    )

    val httpClient =
      HttpClientBuilder.create()
        .setDefaultCredentialsProvider(credentialsProvider)
        .build()

    val httpResponse = new HttpGet(api_url)
    httpResponse.setHeader("Content-type", "application/json")
    httpClient.execute(httpResponse)
//how to pass my LD-Json data here?
  }
}

This is my first question here. If it's too trivial, please excuse me. I am trying to write a scraper class in Scala, which fetches LD-Json from a URL and posts that to an external API.

1

There are 1 best solutions below

1
On

You should use HttpClientBuilder.

Simple example:

String url = "http://example.com";

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);

HttpResponse response = client.execute(request);

System.out.println("Result: " + response.getStatusLine().getStatusCode());

BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent()));

StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
        result.append(line);
}