- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Java 构建一个简单的 HTTP Web 代理。现在我所拥有的就是在端口 5041 上监听 HTTP 请求的东西。现在我有2节课。 Proxy
类在端口上打开一个 ServerSocket
,我将此套接字称为 welcomeSocket
。打开此端口后,每次 welcomeSocket
接受新套接字时,Proxy
都会启动一个新的 Connection
线程。 Connection
类有一些简单的逻辑来读取 HTTP 请求 header 信息。稍后将扩展该类以获得更多功能。感谢您的帮助。
当我运行代理并将浏览器配置为指向代理时,我收到 SocketException
错误,表明我的套接字已关闭。我不确定这些是怎么发生的。此外,Connection
线程似乎正在创建,但不一定输出 HTTP header 信息。这是显而易见的,因为输出显示重复的 NEW connection thread created
打印,但打印后没有标题信息。
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Proxy {
// Port is 5000 + 41
private final int PORT;
private ServerSocket welcomeSocket = null;
private boolean listening = true;
public Proxy(int port) {
PORT = port;
try {
welcomeSocket = new ServerSocket(PORT);
listenForConnectionRequests();
} catch (IOException e) {
System.err.println("There was a problem while creating the welcome socket");
e.printStackTrace();
System.exit(-1);
}
}
public void listenForConnectionRequests() {
System.out.println("Listening for connection requests at " + PORT + "\n");
while (listening) {
try {
new Connection(welcomeSocket.accept()).start();
} catch (IOException e) {
System.err.println("I/O error occured while waiting for a connection.");
e.printStackTrace();
}
}
}
}
public class Connection extends Thread {
private Socket connectionSocket = null;
private ArrayList<String> requestHeaders = new ArrayList<String>();
private boolean running = true;
private OutputStream os;
private InputStream is;
public Connection(Socket connectionSocket) {
super("Proxy");
System.out.println("NEW connection thread created");
this.connectionSocket = connectionSocket;
}
public void run() {
while (running) {
readHttpHeader();
}
}
private void stopRunning() {
running = false;
System.err.println("Stopping thread.");
closeEverything();
}
private void readHttpHeader() {
try {
os = connectionSocket.getOutputStream();
} catch (IOException e) {
System.err.println("Problem getting output stream from connection socket.");
e.printStackTrace();
stopRunning();
}
try {
is = connectionSocket.getInputStream();
} catch (IOException e) {
System.err.println("Problem getting input stream from connection socket.");
e.printStackTrace();
stopRunning();
}
String input;
BufferedReader in = null;
DataOutputStream out = null;
try {
in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
out = new DataOutputStream(connectionSocket.getOutputStream());
while ((input = in.readLine()) != null) {
requestHeaders.add(input);
System.out.println(input);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (in != null)
in.close();
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void closeEverything() {
try {
connectionSocket.close();
is.close();
os.close();
} catch (IOException e) {
System.err.println("Problem closing connection or streams");
e.printStackTrace();
stopRunning();
}
}
}
Listening for connection requests at 5041
NEW connection thread created
GET http://127.0.0.1:63342/browserConnection/buildInfo HTTP/1.1
Host: 127.0.0.1:63342
Proxy-Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
NEW connection thread created
NEW connection thread created
GET http://google.com/ HTTP/1.1
Host: google.com
Proxy-Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
X-Client-Data: CKK2yQEIwbbJAQj9lcoB
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: OGP=-5061574:; SID=DQAAAMoCAACmU0EuiAZrQAuo8mgssp5cukiLvNn13ryGL2GeGswaF8Cmv2NIx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nzeqNC_fwNSDElB7qfUmmvnZSHX1mowllXfpIM8cLm-ewYD0tGdHbpfvbAxkfH_jIwQKj-pS0f6ZkcodJ2CEEeoDhMUbZDAOfBKUUhM5qvLxE3curLhef6pftojQo0QpTumhoky2_pWR1lWP-5W0lPN29lMDSdeYrW5Umz_3jNHPRyMiGFwzNMIV9r9RUoyO50JreK057mDnzNygESvk_fBHlt49b2U6tesQ5NAtaAmL4VaWsAuj3jKEvjWTxSgSVrUeWwPC84hjn3liuGlBulWg; OGPC=5061900-22:5061918-27:5061934-15:5061921-40:5061937-46:5061933-25:5061941-6:5061574-1:5061940-57:5061952-26:5061975-14:5061983-7:
Problem getting output stream from connection socket.
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:916)
at Connection.readHttpHeader(Connection.java:46)
at Connection.run(Connection.java:34)
Stopping thread.
Problem getting input stream from connection socket.
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:876)
at Connection.readHttpHeader(Connection.java:54)
at Connection.run(Connection.java:34)
Stopping thread.
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:876)
at Connection.readHttpHeader(Connection.java:65)
at Connection.run(Connection.java:34)
Problem getting output stream from connection socket.
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:916)
at Connection.readHttpHeader(Connection.java:46)
at Connection.run(Connection.java:34)
Stopping thread.
Problem getting input stream from connection socket.
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:876)
at Connection.readHttpHeader(Connection.java:54)
at Connection.run(Connection.java:34)
Stopping thread.
java.net.SocketException: Socket is closed
at java.net.Socket.getInputStream(Socket.java:876)
at Connection.readHttpHeader(Connection.java:65)
at Connection.run(Connection.java:34)
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
NEW connection thread created
GET http://www.yelp.com/ HTTP/1.1
Host: www.yelp.com
Proxy-Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: __cfduid=dd3ccfb39916d84ca8053ba95c0ca89fe1457229239; yuv=pOdmTZnrxWdr1NhFcLyF8cCB7Pj5l2tN3CSqNp0fiz0yKun0fbD0MHFrIo4K432S0pKC_-Sl9QijvKAX5u1iUJMmwZ2EnL4p; hl=en_US; fd=0; D_SID=172.250.60.189:SscPaGXtn/I/Skx3ISxQZOtF5qtsm+TmywEwnqXsiuo; qntcst=D%2CT%2C43606%2C43604%2C43602%2C43597%2C43596%2C43595%2C43583%2C43582%2C43581%2C37336%2C34588%2C34585%2C27425%2C27424%2C20052%2C20047%2C20040%2C20021%2C20020%2C20015%2C19991%2C19989%2C19958%2C19948%2C19947%2C19945%2C19944%2C19942%2C19941%2C19939%2C19936%2C19934%2C19932; __qca=P0-1476739725-1457229248633; fbm_97534753161=base_domain=.yelp.com; location=%7B%22city%22%3A+%22Seattle%22%2C+%22zip%22%3A+%22%22%2C+%22country%22%3A+%22US%22%2C+%22address2%22%3A+%22%22%2C+%22address3%22%3A+%22%22%2C+%22state%22%3A+%22WA%22%2C+%22address1%22%3A+%22%22%2C+%22unformatted%22%3A+%22seattle%22%7D; bse=b955bbc7020253479a51b278e343d766; __utma=165223479.439363298.1457229244.1457383227.1457394139.4; __utmc=165223479; __utmz=165223479.1457383227.3.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __utmv=165223479.|4=account%20level=anon=1; _ga=GA1.2.C1BEB9B4D17FEEDD; D_PID=469F0452-18FF-3E05-8072-566D9785BE96; D_IID=EC337894-ADD2-36A6-848D-AA87DA4B1407; D_UID=6E67A9C8-F999-33A6-8838-4EBFBC9B943E; D_HID=qXtXeOzvXZDnGT33GPaUewyWk7KgXdgunNy1mqdkX7g
Problem getting output stream from connection socket.
Problem getting output stream from connection socket.
Problem getting output stream from connection socket.
Problem getting output stream from connection socket.
最佳答案
您正在关闭套接字,然后尝试从中读取数据。
下面的代码将调用readHttpHeader两次...
while (running) {
readHttpHeader();
}
第一次成功:
try {
in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
while ((input = in.readLine()) != null) {
requestHeaders.add(input);
System.out.println(input);
}
然后关闭输入流...
} finally {
try {
if (in != null)
in.close();
关闭它所包装的流(InputStreamReader
),进而关闭套接字的输入流。
然后循环第二次调用 readHttpHeader(),这会发现流已关闭。几个 try-catch block 捕获一些错误,将它们报告给 stderr,然后继续前进,到达读取循环(失败),最后关闭流。最后,由于 running
为 false
,因此不会第三次调用 readHttpHeader()
。
关于java - 简单 Java Web 代理 SocketException : Socket is closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35877634/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!