gpt4 book ai didi

使用套接字的 Java 代理

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:19 29 4
gpt4 key购买 nike

我在使用此代码时遇到了一些问题,具体取决于我使用的浏览器,有些 URL 在 IE 中显示正确,但在 Firefox 中显示为纯文本(例如 www.microsoft.es 在 IE 上看起来不错,但在 Firefox 上则不然) )。不知道我在这里做错了什么,我认为我正在使用的 header 有问题,但我不确定......

这是代码:

import java.io.*;
import java.net.*;
import java.util.concurrent.*;


public class Server {


public void startServer() {
final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);

Runnable serverTask = new Runnable() {
@Override
public void run() {
try {
@SuppressWarnings("resource")
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket clientSocket = serverSocket.accept();
clientProcessingPool.submit(new ClientTask(clientSocket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread serverThread = new Thread(serverTask);
serverThread.start();

}

private class ClientTask implements Runnable {
private Socket clientSocket;

private ClientTask(Socket clientSocket) {
this.clientSocket = clientSocket;
}


@Override
public void run() {

try {


BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedOutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());

String url = null;
int i=0;
String [] headers = new String [100];
String buffer;

while ((buffer = in.readLine()) != null) {
headers[i]=buffer;
i++;

if(buffer.contains("GET"))
{
String[] splitText = buffer.split(" ");
url = splitText[1];
}
if(buffer.contains("POST"))
{
String[] splitText = buffer.split(" ");
url = splitText[1];
}
if(buffer.contains("CONNECT"))
{
String[] splitText = buffer.split(" ");
url = "https://"+splitText[1];
}
if (buffer.isEmpty()) break;
}

URL u = new URL(url);
URLConnection connection = u.openConnection();

for (int x=1;x<i-1;x++){
if (!headers[x].contains("Accept-Encoding:")){
connection.setRequestProperty(headers[x].substring(0, headers[x].indexOf(":")).toString() , headers[x].replace(headers[x].substring(0, headers[x].indexOf(":") +2), "").toString());
}
}

boolean redirect = false;

int status = ((HttpURLConnection) connection).getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}


if (redirect) {

String Url = connection.getHeaderField("Location");
URL urlloc = new URL(Url);
connection = urlloc.openConnection();

for (int x=1;x<i-1;x++){

if (!headers[x].contains("Accept-Encoding:")){
connection.setRequestProperty(headers[x].substring(0, headers[x].indexOf(":")).toString() , headers[x].replace(headers[x].substring(0, headers[x].indexOf(":") +2), "").toString());
}
}
}


byte[] chunk = new byte[1024];
int bytesRead;
InputStream stream;
stream = connection.getInputStream();
while ((bytesRead = stream.read(chunk)) > 0) {
out.write(chunk, 0, bytesRead);
out.flush();
}


out.close();
in.close();
clientSocket.close();


} catch (Exception e) {
e.printStackTrace();

}
}
}

}

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

我不确定问题是什么,但您应该使用像 Wireshark 这样的工具来检查浏览器和代理之间的实际网络流量,并将其与连接时浏览器和站点之间的网络流量进行比较直接访问该网站。

关于使用套接字的 Java 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20292022/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com