package cn.dzr.uploadpic;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class MyBrowser
{
public static void main(String[] args) throws IOException, IOException
{
Socket socket = new Socket(InetAddress.getLocalHost(),8080);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
pw.println("GET /myweb/1.html HTTP/1.1");
pw.println("Accept : */*");
pw.println("Host: 0.0.0.0:8080");
pw.println("Connection : close");
pw.println();
pw.println();
InputStream in = socket.getInputStream();
byte[] buff = new byte[1024];
int len = in.read(buff);
String str = new String(buff,0,len);
System.out.println(str);
socket.close();
}
}
提示错误如下:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
buff就没有读取到数据么?
服务器是正常的,我通过浏览器就能够访问。
------解决方案--------------------
我也不懂 刚自己试了下
1、
pw.println();
pw.println();
后面加个flush
2、
while((len=in.read(buff))!=-1)
{
System.out.println(len);
String str = new String(buff,0,len);
System.out.println(str);
socket.close();
}
改成
while((len=in.read(buff))!=-1)
{
System.out.println(len);
String str = new String(buff,0,len);
System.out.println(str);
}
socket.close();