http传输速度的问题

挨踢民工的乐章 2008-08-04 03:32:34
用urlConnection建立http连接,怎么才可以提高传输的速度?

String strResponse = "";
URL url;
URLConnection urlConnection;

url = new URL(m_strUrl);

urlConnection = url.openConnection(); //--------这里要耗时16ms
urlConnection.setDoOutput(true); // 需要向服务器写数据
urlConnection.setDoInput(true); //
urlConnection.setUseCaches(false); // 获得服务器最新的信息
urlConnection.setAllowUserInteraction(false);
urlConnection.setReadTimeout(50);
urlConnection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
// 必须告诉服务器你发送的数据大小. 这也同样告诉
urlConnection.setRequestProperty("Content-length", ""
+ m_strRequest.length());
// Open an output stream so you can send the info you are posting
try
{ //-----------这里要16ms
dataOutputStream = new DataOutputStream(urlConnection.getOutputStream());
}
catch(Exception e)
{
System.out.println(url.toString()+":连接超时!");
return "";
}

// Write out the actual request data
dataOutputStream.writeBytes(m_strRequest);
dataOutputStream.close();
// 判断访问服务器的返回值
boolean loop=true;
InputStreamReader netin=new InputStreamReader(urlConnection.getInputStream());
BufferedReader in=new BufferedReader(netin);
String str="";

while (loop) //这里要16ms {
if (in.ready())
{
while (true)
{
str = in.readLine();
if (str == null)
{
loop = false;
break;
}
strResponse += str;
}
}
}
return strResponse;
}



}

红色的是传输7k文件所要消耗的时间,有没有什么方法可以提高传输的速度?是不是有更好的代码。
希望高手指教,现在这里的速度是我们软件的瓶颈。。。
...全文
132 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
胡矣 2008-08-04
  • 打赏
  • 举报
回复
这个和你网络的速度
返回文件的大小都有关系
请问LZ是如何测试得到的速度
dryZeng 2008-08-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kokobox 的回复:]
这也跟网络和带宽有关系,而且你测试的 时间不一定是准确的
[/Quote]

支持
kokobox 2008-08-04
  • 打赏
  • 举报
回复
这也跟网络和带宽有关系,而且你测试的 时间不一定是准确的

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧