在Java网络编程中,代理服务器是一种中介服务器,它位于客户端和目标服务器之间,负责接收客户端的请求并将其转发到目标服务器。代理服务器可以用于多种目的,如隐藏客户端的真实IP地址、缓存请求结果、过滤内容、实现安全监控等。
// 目标对象
public class UserImpl {
public void system() {
System.out.println("输出测试");
}
}
// 代理对象
public class Proxy extends UserImpl {
@Override
public void system() {
super.system();
System.out.println("增强之后的输出");
}
}
// 测试类
public class TestMain {
public static void main(String[] args) {
UserImpl user = new Proxy();
user.system();
}
}
// 接口类
public interface Italk {
void talk(String msg);
}
// 实现类
public class People implements Italk {
public String username;
public String age;
public String getName() {
return username;
}
public void setName(String name) {
this.username = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public People(String name1, String age1) {
this.username = name1;
this.age = age1;
}
public void talk(String msg) {
System.out.println(msg + "!你好,我是" + username + ",我年龄是" + age);
}
}
// 代理类
public class TalkProxy implements Italk {
Italk talker;
public TalkProxy(Italk talker) {
this.talker = talker;
}
public void talk(String msg) {
talker.talk(msg);
}
public void talk(String msg, String singname) {
talker.talk(msg);
sing(singname);
}
private void sing(String singname) {
System.out.println("唱歌:" + singname);
}
}
// 测试
public class MyProxyTest {
public static void main(String[] args) {
Italk people1 = new People("湖海散人", "18");
people1.talk("No ProXY Test");
System.out.println("-----------------------------");
TalkProxy talker = new TalkProxy(people1);
talker.talk("ProXY Test", "七里香");
}
}
以下是一个简单的Java HTTP代理服务器的示例代码:
import java.io.*;
import java.net.*;
public class SimpleProxyServer {
public static void main(String[] args) throws IOException {
int port = 8080; // 代理服务器监听的端口
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("代理服务器正在运行, 监听端口: " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(new ProxyThread(clientSocket)).start(); // 每个连接启动一个新线程处理
}
}
}
}
class ProxyThread implements Runnable {
private Socket clientSocket;
public ProxyThread(Socket clientSocket) {
this.clientSocket = clientSocket;
}
@Override
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true)) {
// 读取客户端请求
String requestLine = in.readLine();
System.out.println("请求: " + requestLine);
String[] requestParts = requestLine.split(" ");
String method = requestParts[0];
String url = requestParts[1];
// 解析目标地址
URL targetUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection();
connection.setRequestMethod(method);
// 读取目标服务器响应
int responseCode = connection.getResponseCode();
InputStream responseStream = connection.getInputStream();
BufferedReader responseReader = new BufferedReader(new InputStreamReader(responseStream));
// 返回响应给客户端
out.println("HTTP/1.1 " + responseCode + " " + connection.getResponseMessage());
String line;
while ((line = responseReader.readLine()) != null) {
out.println(line);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientProxyExample {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://example.com");
// 设置代理
httpGet.setURI(new URI("http://proxy.example.com:8080"));
// 发送请求
CloseableHttpResponse response = httpClient.execute(httpGet);
// 获取响应实体
HttpEntity entity = response.getEntity();
// 将响应内容读取到字符串中
String content = EntityUtils.toString(entity);
// 关闭资源
response.close();
httpClient.close();
System.out.println(content);
}
}
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.proxy.Proxy;
import java.net.InetSocketAddress;
public class OkHttpProxyExample {
public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080)))
.build();
Request request = new Request.Builder()
.url("http://www.example.com")
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.body().string());
}
}
}
希望这些示例和解释能帮助你更好地理解和使用Java中的代理服务器。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。