无法在小程序中 getInputStream()
大家好,
我正在尝试用 Java 编写一个小程序,它将访问视频游戏的应用程序编程接口 (API),虽然我可以通过 Eclipse IDE 成功运行该小程序,但从浏览器运行时它始终挂起。
我通过分散调试消息直到找到最后一行运行来缩小错误的范围。
我正在尝试解析参数填充 URL 的输出。考虑到浏览器必须以不同于 IDE 的方式传递此信息,我尝试了许多不同的方法来执行此操作,包括通过 http 套接字 POST 参数(尽管我不熟悉此方法,并且很容易错误地实现它)。下面是我当前的版本,省略了不相关的部分(如果您应该推断出错误可能在省略的区域中,这些很容易被发现):
...
URL apiCharList = null;
...
try {
...
apiCharList = new URL("https:// ... ");
URLConnection connection = apiCharList.openConnection();
connection.connect();
DataInputStream in = new DataInputStream( new BufferedInputStream( connection.getInputStream() ) );
BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
String line = br.readLine();
while( line != null ) {
...
line = br.readLine();
}
...
} catch (MalformedURLException e) {
current.setText( "Malformed URL" );
} catch (IOException e) {
current.setText( "InputStream fail." );
}
我的调试让我在:
connection.connect();
没有那一行,在:
DataInputStream in = new DataInputStream( new BufferedInputStream( connection.getInputStream() ) );
对这个问题的任何洞察都是最值得赞赏的。再次,请让我知道是否/哪些省略的区域可能需要查看。
尊敬, 询问
更新
谢谢大家的回复。我的 BufferedReader 现在初始化为:
= new BufferedReader( new InputStreamReader( connection.getInputStream() ), 1 );
该部分作为我尝试过的所有各种方法的组合而不断跳跃;谢谢你帮我解决了这个问题。从我所看到的来看,问题似乎是我的小程序需要进行数字签名,以便在通过浏览器运行时建立所需的连接。我一直在研究这个问题,并且在下载最新的 JDK 后,keytool 和 jarsigner 出现了问题。我只接触了很短一段时间,以前从未有过数字签名的小程序,但至少我有一个新的追求途径。如果有人能够提供有关如何对小程序进行数字签名的良好(最新)演练,我们将不胜感激。我读过的各种文章都说它可能会花费我 40 美元到 20 美元甚至零?
无论如何,感谢您克服了这个障碍。 谨此, 查询
Everyone,
I am trying to code an applet in Java which will access a video-game's Application Programming Interface (API), and while I can successfully run the applet via the Eclipse IDE, it consistantly hangs up when run from the browser.
I've narrowed down where the bug must be by scattering debug messages around until I found the last line run.
I am attempting to parse the output from a parameter-filled URL. Figuring browsers must pass this information differently than an IDE, I've tried many different methods of doing this, including POSTing the parameters via http socket (although I am unfamiliar with this method and could easily have implemented it incorrectly). Below is my current version, irrelevent parts omitted (if you should deduce the bug might be in an omitted area, these are easily revealed):
...
URL apiCharList = null;
...
try {
...
apiCharList = new URL("https:// ... ");
URLConnection connection = apiCharList.openConnection();
connection.connect();
DataInputStream in = new DataInputStream( new BufferedInputStream( connection.getInputStream() ) );
BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
String line = br.readLine();
while( line != null ) {
...
line = br.readLine();
}
...
} catch (MalformedURLException e) {
current.setText( "Malformed URL" );
} catch (IOException e) {
current.setText( "InputStream fail." );
}
My debugging dropped me at:
connection.connect();
And without that line, at:
DataInputStream in = new DataInputStream( new BufferedInputStream( connection.getInputStream() ) );
Any insight into this problem is most appreciated. Again, simply let me know if/which omitted areas may be necessary to see.
Respectfully,
Inquiring
UPDATE
Thank you all for the replies. My BufferedReader now initializes as:
= new BufferedReader( new InputStreamReader( connection.getInputStream() ), 1 );
That part had been getting jumpbled up as a combination of all the various methods I had tried; thank you for ironing it out for me. From what I am seeing, it seems the issue is that my applet needs to be digitally signed in order to make the connections it requires when run via browser. I've been looking into that and have been having problems with keytool and jarsigner after downloading the latest JDK. I have only been at it for a short while, and have never had an applet digitally signed before, but at least I have a new avenue to pursue. If anyone caould provide a good (up to date) walkthrough on how to digitally sign an applet, that would be most appreciated. Various things I've read said it can cost me anywhere from 40USD to 20USD to nothing?
Anyways, thanks for the lift over this hurdle.
Respectully,
Inquiring
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,浏览器不允许小程序在除其原始服务器之外的任何地方建立网络连接。您需要对小程序进行数字签名,然后请求用户允许进行连接。另一种方法是在 Web 服务器上运行一个简单的代理 servlet,小程序可以使用该代理 servlet 与第三方服务器进行通信。
顺便说一句,为什么你要互相缠绕这么多流 - 其中两个是缓冲的,这是一个巨大的禁忌 - 只是为了从网络读取?这两行可以替换为
“请注意,我在末尾插入了“,1””,这会关闭 BufferedReader 的缓冲。您不想提前阅读网络连接,或者您正在邀请您的代码挂起。
Browsers will not allow applets to make network connections anywhere but their own server of origin, by default. You would need to digitally sign the applet, then ask the user for permission to make the connection. An alternative is to run a simple proxy servlet on your web server which the applet can use to talk to the third-party server.
As an aside, why are you wrapping so many streams around eachother -- two of them buffered, which is a huge no-no -- just to read from the network? Those two lines could be replaced by
Do note that ",1" I stuck in at the end, which turns off buffering for the BufferedReader. You do not want to read ahead on a network connection, or you're inviting your code to hang.