收到403错误响应httpclient java(HTTP / 1.1 403 Forbidden)

时间:2020-03-26 12:34:14

标签: java proxy httpclient

我正在尝试编写一个简单的机器人,以使用代理进行连接(中间连接),并使用apache httpclient从Facebook之类的网站获得响应。但是我不知道为什么会收到类似响应这样的错误:HTTP / 1.1 403 Forbidden Access Denied。

我试图在请求中添加标头,但它不起作用。

请帮助

提前致谢

******代码********

package bot;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.util.EntityUtils;

public class Demo2 {

    public static void main(String[] args) {
        HttpHost proxyhost = new HttpHost("54.164.133.248", 3128);

        // Creating an HttpHost object for target

        HttpHost targethost = new HttpHost("facebook.com",443);


        // creating a RoutePlanner object
        HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyhost);

        // Setting the route planner to the HttpClientBuilder object
        HttpClientBuilder clientBuilder = HttpClients.custom();

        clientBuilder = clientBuilder.setRoutePlanner(routePlanner);

        // Building a CloseableHttpClient
        CloseableHttpClient httpclient = clientBuilder.build();

        // Creating an HttpGet object
        HttpGet httpget = new HttpGet("/");
        httpget.setHeader("scheme", "https");
        httpget.setHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        httpget.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36");
        httpget.setHeader("Connection", "keep-alive");

        try {
            // Executing the Get request
            HttpResponse httpresponse = httpclient.execute(targethost, httpget);

            // Printing the status line
            System.out.println("httpresponse.getStatusLine() : " + httpresponse.getStatusLine());

            // Printing all the headers of the response
            Header[] headers = httpresponse.getAllHeaders();

            for (int i = 0; i < headers.length; i++) {
                System.out.println("header " + i + " : " + headers[i]);
            }

            // Printing the body of the response
            HttpEntity entity = httpresponse.getEntity();

            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

0 个答案:

没有答案