简体   繁体   中英

Not all sites can be proxied by java NIO. Why?

I have implemented simple proxy server using Java NIO channels, but have a problem, some sites works perfectly, but other give an error about unknown path or redirect on technical page of its hoster with message the resource doesn't exist. Is it my fault or may be some sites don`t allow proxy?

ProxyServer works as this: I enter 'localhost' and in browser I recive site that was set in code. And request from browser I simply resend on target site at such way:

private void connect(SelectionKey key) throws IOException {
    SocketChannel channel = ((SocketChannel) key.channel());
    Attachment attachment = (Attachment) key.attachment();
    channel.write(attachment.buffer);
}

So 'key' - is SelectionKey of target site and in attachment.buffer I store request that was send to proxy server.

So, something worng with my code or its just closed opportunity to proxy by sites?

Update 1. I suppose, I found a problem. Cause I redirect request from localhost to remote server AS IS so in request in field HOST I have 'localhost'. It seems like some sites ignore this fields, other try to use and redirect to 404 page, cause can't find 'localhost' I`m asking for. So question is how to change field 'Host' in request on destination server name?

The target server doesn't know anything about your NIO code, or whether you are a proxy or a real client.

If you got an error page, the proxy is working, and it is the resource being proxied that is the problem: it doesn't exist, you don't have access, etc. Nothing you can do about that in your code and no reason why you should worry. Just send the error page to the client, same as you would send anything else.

Why is that method called connect() when it doesn't connect and does do something else?

I found a problem. filed HOST after proxy contains 'localhost', so some sites accept it, other not. Replace value of this fields with real host fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM