菜鸟的写的一段有问题的Java代码!为什么从网页上获取的源文件写不到我创建的文本文档中,控制台可以显示
以下是我写的代码,请高手指点为什么我创建的文本文档是空的,字符流写不进去啊!!!importjava.io.*;importjava.net.*;publicclassT...
以下是我写的代码,请高手指点为什么我创建的文本文档是空的,字符流写不进去啊!!!import java.io.*;import java.net.*;public class TestHttp { /** * @param args */ public static void main(String[] args) { try{ Socket sk=new Socket(InetAddress.getByName("www.hao123.com"),80); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())); //bw.write("GET / HTTP/1.1"); bw.write("POST / HTTP/1.1"); bw.newLine(); bw.write("Host: www.hao123.com:80"); bw.newLine(); bw.write("Content-Type: text-html"); bw.newLine(); bw.newLine(); bw.flush(); BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream())); String str=null; File f=new File("E:\\","newFile.txt"); while((str=br.readLine())!=null){ FileOutputStream fos=new FileOutputStream(f); BufferedOutputStream bos=new BufferedOutputStream(fos); bos.write(str.getBytes()); System.out.println(str); } bw.close(); br.close(); sk.close(); }catch(Exception e){ } }}
展开
2个回答
展开全部
试一下这个
import java.io.*;
import java.net.*;
public class TestHttp
{
/** * @param args */
public static void main(String[] args)
{
try{
Socket sk=new Socket(InetAddress.getByName("www.hao123.com"),80);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
//bw.write("GET / HTTP/1.1");
bw.write("POST / HTTP/1.1");
bw.newLine();
bw.write("Host: www.hao123.com:80");
bw.newLine();
bw.write("Content-Type: text-html");
bw.newLine();
bw.newLine();
bw.flush();
BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream()));
String str=null;
File f=new File("E:\\","newFile.txt");
FileOutputStream fos=new FileOutputStream(f);
while((str=br.readLine())!=null)
{
fos.write(str.getBytes());
System.out.println(str);
}
bw.close();
br.close();
sk.close();
}
catch(Exception e){ }
}
}
import java.io.*;
import java.net.*;
public class TestHttp
{
/** * @param args */
public static void main(String[] args)
{
try{
Socket sk=new Socket(InetAddress.getByName("www.hao123.com"),80);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
//bw.write("GET / HTTP/1.1");
bw.write("POST / HTTP/1.1");
bw.newLine();
bw.write("Host: www.hao123.com:80");
bw.newLine();
bw.write("Content-Type: text-html");
bw.newLine();
bw.newLine();
bw.flush();
BufferedReader br=new BufferedReader(new InputStreamReader(sk.getInputStream()));
String str=null;
File f=new File("E:\\","newFile.txt");
FileOutputStream fos=new FileOutputStream(f);
while((str=br.readLine())!=null)
{
fos.write(str.getBytes());
System.out.println(str);
}
bw.close();
br.close();
sk.close();
}
catch(Exception e){ }
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询