我用socket访问https.代码到最后就等在那里要很长时间才读完。我的代码如下: Writer out=null;
InputStream ina=null;
BufferedReader in=null;
Socket socket=null;
try {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
String tunnelHost = "www.aa.com.cn";
int tunnelPort = 8443;
Socket tunnel = new Socket(tunnelHost, tunnelPort);
socket = factory.createSocket(tunnel, tunnelHost, tunnelPort, true);
out = new OutputStreamWriter(socket.getOutputStream(),"GBK");
out.write("POSE https://www.aa.com.cn:8443/spservice/spgetringinfo.do?spid=xxx&sppwd=xxxx HTTP/1.1\r\n");
out.write("Host: " + "61.112.123.23" + ":" + 443 + "\r\n");
out.write("\r\n");
out.flush();
ina = socket.getInputStream();
in = new BufferedReader(new InputStreamReader(ina, "GBK"));
String line = null;
String str = in.toString();
----〉〉〉在这里每次读到返回值最后的时候就特别慢。前面readline()都读得特别快!不知道为什么?
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}catch(Exception e){
System.out.println("error +"+e);
}finally {
if(in!=null)
in.close();
if(ina!=null)
ina.close();
if(out!=null)
out.close();
socket.close();
}
InputStream ina=null;
BufferedReader in=null;
Socket socket=null;
try {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
String tunnelHost = "www.aa.com.cn";
int tunnelPort = 8443;
Socket tunnel = new Socket(tunnelHost, tunnelPort);
socket = factory.createSocket(tunnel, tunnelHost, tunnelPort, true);
out = new OutputStreamWriter(socket.getOutputStream(),"GBK");
out.write("POSE https://www.aa.com.cn:8443/spservice/spgetringinfo.do?spid=xxx&sppwd=xxxx HTTP/1.1\r\n");
out.write("Host: " + "61.112.123.23" + ":" + 443 + "\r\n");
out.write("\r\n");
out.flush();
ina = socket.getInputStream();
in = new BufferedReader(new InputStreamReader(ina, "GBK"));
String line = null;
String str = in.toString();
----〉〉〉在这里每次读到返回值最后的时候就特别慢。前面readline()都读得特别快!不知道为什么?
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}catch(Exception e){
System.out.println("error +"+e);
}finally {
if(in!=null)
in.close();
if(ina!=null)
ina.close();
if(out!=null)
out.close();
socket.close();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货