Java - 最快的方式,以及加载URL并从服务器获取响应的最佳代码 [英] Java - Fastest way, and best code to load a URL and get a response from the server

查看:127
本文介绍了Java - 最快的方式,以及加载URL并从服务器获取响应的最佳代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇从服务器获取响应的最佳和最快的方式,比如我是否使用for循环加载返回XML文件的url,我可以使用哪种方式加载url get连续10次回复?速度是最重要的。我知道它只能像你的互联网一样快,但我需要一种方法来加载网址,就像我的互联网允许的那样快,然后把网址的谁输出放在一个字符串中,这样我就可以追加到JTextArea ..这就是代码我一直在使用,但如果可能的话寻求更快的替代方案

I was curious as to what was the best and FASTEST way to get a response from the server, say if I used a for loop to load a url that returned an XML file, which way could I use to load the url get the response 10 times in a row? speed is the most important thing. I know it can only go as fast as your internet but I need a way to load the url as fast as my internet will allow and then put the who output of the url in a string so i can append to JTextArea.. This is the code Ive been using but seek faster alternatives if possible


int times = Integer.parseInt(jTextField3.getText());

int times = Integer.parseInt(jTextField3.getText());

            for(int abc = 0; abc!=times; abc++){
                try {
                        URL gameHeader = new URL(jTextField2.getText());
                        InputStream in = gameHeader.openStream();
                        byte[] buffer = new byte[1024];
                    try {
                        for(int cwb; (cwb = in.read(buffer)) != -1;){
                            jTextArea1.append(new String(buffer, 0, cwb));
                        } 
                    } catch (IOException e) {}
                } catch (MalformedURLException e) {} catch (IOException e) {}
            }

有什么比这个?

谢谢

-CLUEL3SS

-CLUEL3SS

推荐答案

使用BufferedReader;使用比1024更大的缓冲区大小;不要吞下例外。您也可以尝试重复使用相同的URL对象,而不是每次都创建一个新对象,可能有助于连接池。

Use a BufferedReader; use a much larger buffer size than 1024; don't swallow exceptions. You could also try re-using the same URL object instead of creating a new one each time, might help with connection pooling.

但为什么要读取相同内容网址连续10次?

But why would you want to read the same URL 10 times in a row?

这篇关于Java - 最快的方式,以及加载URL并从服务器获取响应的最佳代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆