java通过代理使用SokSet获取Html
我有一个Java代码,允许通过代理服务器从网站获取HTML:
import java.net.*;
import java.io.*;
import java.util.Base64;
import javax.net.ssl.*;
public class example {
public static void main(String[] args) throws Exception {
new example().request("stackoverflow.com", "/enterprise", 443);//It is just example
}
public void request(String host, String path, int port) throws Exception {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket tunnel = new Socket("myproxyserver.com", 22225);//It is just example
String auth = "Admin:PASS";//It is just example
String base64 = Base64.getEncoder().encodeToString(auth.getBytes());
OutputStream os = tunnel.getOutputStream();
String msg = "CONNECT "+host+":"+port+" HTTP/1.1\n"
+"Proxy-Authorization: Basic "+base64+"\n"
+"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
+"\r\n\r\n";
byte b[] = msg.getBytes("ASCII7");
os.write(b);
os.flush();
int lines = 0;
InputStream is = tunnel.getInputStream();
while (lines<2) {
int i = is.read();
if (i<0)
throw new IOException("Unexpected EOF from proxy");
if (i=='\n')
++lines;
else if (i!='\r')
lines = 0;
}
SSLSocket socket = (SSLSocket) factory.createSocket(tunnel, host, port, true);
socket.startHandshake();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())));
out.println("GET "+path+" HTTP/1.0");
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
out.close();
socket.close();
tunnel.close();
}
}
我想用C#做同样的事情。我找到了以下代码,但它不起作用:
string server = "myproxyserver.com";
int port = 22225;
string host = "https://stackoverflow.com/enterprise";
var base64 = "bHVtLWN1c4RvbWVyLWhs4zM4HTI2N2M2LXpvbmUtc3RhdGljEmU3ZWN5Y3ByY2U2YQ==";
string request = "GET / HTTP/1.1\r\nHost: " + host + ":" + port
+ "\r\nProxy-Authorization: Basic " + base64 + "\n"
+ "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
+ "\r\n\r\n";
Byte[] requestBytes = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];
IPHostEntry hostEntry = Dns.GetHostEntry(server);
var ipe = new IPEndPoint(hostEntry.AddressList[0], port);
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Connect(ipe);
if (socket.Connected)
{
Console.WriteLine("Connection established");
}
else
{
Console.WriteLine("Connection failed");
return;
}
socket.Send(requestBytes, requestBytes.Length, 0);
int bytes = 0;
var sb = new StringBuilder();
do
{
bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0);
sb.Append(Encoding.ASCII.GetString(bytesReceived, 0, bytes));
}
while (bytes > 0);
Console.WriteLine(sb.ToString());
它正在返回字符串,但我期待HTML:
Connection established
HTTP/1.1 403 No Protocol
X-Luminati-Error: No Protocol
Date: Thu, 30 Jul 2020 03:26:38 GMT
Connection: keep-alive
Transfer-Encoding: chunked
我该怎么解决这个问题? 更新 我想通过WebClient完成,但它抛出了超时异常:
var client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Proxy = new WebProxy("myproxyserver:22225");
client.Proxy.Credentials = new NetworkCredential("Admin", "Password");
Console.WriteLine(client.DownloadString("https://stackoverflow.com/enterprise"));
然而,它在HTTP网站上运行良好。还有,我试着和邮递员一起做这件事。它还抛出了超时异常。当我打开控制台时,我可以看到以下错误:
Error: tunneling socket could not be established, cause=read ECONNRESET
共 (0) 个答案