Подтвердить что ты не робот

Org.apache.http.ProtocolException: целевой узел не указан

Я написал простой код httprequest/response, и я получаю это ниже ошибки. Я ссылался на httpclient, httpcore, common-codecs и common-logging в моем пути к классам. Я очень новичок в java и не знаю, что здесь происходит. Пожалуйста, помогите мне.

код:

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;

public class UnshorteningUrl {

    public static void main(String[] args) throws Exception
    {           
        HttpGet request=null;
        HttpClient client = HttpClientBuilder.create().build();         

        try {
            request = new HttpGet("trib.me/1lBFzSi");
            HttpResponse httpResponse=client.execute(request);

            Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
           // Preconditions.checkState(headers.length == 1);
            String newUrl = headers[0].getValue();          
            System.out.println("new url" + newUrl);         
        } catch (IllegalArgumentException e) {
            // TODO: handle exception
        }finally {
            if (request != null) {
                request.releaseConnection();
            }           
        }
    }}

Ошибка:

Exception in thread "main" org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at UnshorteningUrl.main(UnshorteningUrl.java:26)
Caused by: org.apache.http.ProtocolException: Target host is not specified
    at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:69)
    at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)
    ... 4 more
4b9b3361

Ответ 1

Ошибка является ошибочной. Вы предоставили значение, которое не представляет полный URI

request = new HttpGet("trib.me/1lBFzSi");

Отсутствует протокол.

Просто укажите полный URI

request = new HttpGet("http://trib.me/1lBFzSi");

Ответ 2

Эта ошибка, вероятно, вызвана неправильным URL-адресом. Проверьте URL-адрес:

  • отсутствует протокация;
  • параметры URL-кода кодируются;