我的目标是与服务器建立Java套接字连接,但是有一个HTTP代理阻碍了它。
经过多次测试,我得出结论
System.setProperty(http.proxyHost,"10.126.16.14");
System.setProperty(http.proxyPort,"8080");
System.setProperty(http.proxyUser,"username");
System.setProperty(http.proxyPass,"password");
不会影响通过代理的Socket连接,也不会影响
Socket socket=new Socket(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.126.16.14",8080)));
socket.connect("minds1.no-ip.org",12345);
由于JVM中的错误。 叹息
因此,我的任务是使用Java套接字实现HTTP CONNECT方法请求。 修改了一些代码后,我下了网(抱歉,我找不到源代码!),我现在有了这个方法:
private static Socket createSocket(String address,int port,String proxHost,int proxPort, String proxUser,String proxPass) throws Exception
{
Socket socket=null;
socket = new Socket(proxHost, proxPort); //First step is to connect the socket to the proxy server itself.
String proxRequest="CONNECT "+address+":"+port; //Next step is to send a formatted CONNECT request, including authentication if applicable
if(proxUser!=null&&proxPass!=null)
proxRequest=proxRequest+" HTTP/1.0\nProxy-Authorization: Basic "+toBase64String(proxUser+":"+proxPass)+"=";
proxRequest=proxRequest+"\n\n";
socket.getOutputStream().write(proxRequest.getBytes());
byte[] tmpBuffer = new byte[512]; //Next we read the proxy server's response to see if the request got through.
InputStream in = socket.getInputStream();
int len = in.read(tmpBuffer, 0, tmpBuffer.length);
if (len == 0) throw new SocketException("Invalid response from proxy");
String proxResponse = new String(tmpBuffer, 0, len, "UTF-8");
if (proxResponse.indexOf("200") != -1) //Proxy server has responded that the connection is successful!
{
if (in.available() > 0) in.skip(in.available()); //Cleanup input stream
return socket; //Return the sucessfully-connected socket object
}
else throw new SocketException(proxResponse);
return socket;
}
这是toBase64String方法:
public static char[] Base64CharSet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
public static String toBase64String(String original)
{
byte[] buf=original.getBytes();
int size = buf.length;
char[] ar = new char[((size + 2) / 3) * 4];
int a = 0;
int i = 0;
while (i < size)
{
byte b0 = buf[i++];
byte b1 = (i < size) ? buf[i++] : 0;
byte b2 = (i < size) ? buf[i++] : 0;
int mask = 0x3F;
ar[a++] = Base64CharSet[(b0 >> 2) & mask];
ar[a++] = Base64CharSet[((b0 << 4) | ((b1 & 0xFF) >> 4)) & mask];
ar[a++] = Base64CharSet[((b1 << 2) | ((b2 & 0xFF) >> 6)) & mask];
ar[a++] = Base64CharSet[b2 & mask];
}
switch (size % 3)
{
case 1: ar[--a] = '=';
case 2: ar[--a] = '=';
}
return new String(ar);
}
代码看起来不错,它编译得很好,没有外部库。 但是,调用createSocket方法会从代理服务器返回此响应:
java.net.SocketException: HTTP/1.1 502 Proxy Error ( The specified Secure Socket
s Layer (SSL) port is not allowed. Forefront TMG is not configured to allow SSL
requests from this port. Most Web browsers use port 443 for SSL requests. )
Via: 1.1 E291TMG1
Connection: close
Proxy-Connection: close
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 780
所以我的问题是,这个错误意味着什么,可以通过更改代码来解决问题,如果是,如何解决?