Lowden

now

java 模拟浏览器发出post请求

 

import java.net.*;
import java.io.*;

public class TestofWeiPu {
 private static final String host = http://xxx.xxx.xxx.xx;  
    private static final int port = xxxx;  
    private static final String CONTENT_CHARSET = "GBK";
    Socket socket;
   
    public static void main(String[] args) throws Exception{
        TestofWeiPu client = new TestofWeiPu();
        client.createSocket();
        client.LoginWeiPu();
       
    }
    public void createSocket()throws Exception{
     socket = new Socket("xxx.xxx.xxx.xx",xxxx);
     System.out.println ("CreateSocket");
    }
    public void LoginWeiPu()throws Exception{       
     StringBuffer sb = new StringBuffer("POST  /HTTP/1.1\r\n");
     sb.append("Accept:*/*\r\n");
     sb.append("Referer: \r\n");
     sb.append("Accept-Language: zh-cn\r\n");
     sb.append("Content-Type: application/x-www-form-urlencoded\r\n");
     sb.append("Accept-Encoding: gzip, deflate\r\n");
     sb.append("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n");
     sb.append("Host\r\n");
      sb.append("Content-Length: 259\r\n");
     sb.append("Connection: Keep-Alive\r\n");
     sb.append("Pragma: no-cache\r\n");
     sb.append("Cookie: \r\n\r\n");
     sb.append("body");     
   
     System.out.println (sb.toString());     
     
     OutputStream socketOut = socket.getOutputStream();
     System.out.println (sb.toString().getBytes().length);
     socketOut.write(sb.toString().getBytes()); 
     socketOut.flush();      

   
  BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  String data ="null";
  while(true) {
   data = br.readLine();
   System.out.println (data);
   if(data==null)break;
  }
     
     //System.out.println ("Result:"+data);
  br.close();
  
     socket.close();

     
    }
}

posted on 2009-10-10 16:04 Lowden 阅读(535) 评论(0)  编辑  收藏 所属分类: Java - HtmlParser

My Links

Blog Stats

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

好友链接

搜索

最新评论

阅读排行榜

评论排行榜