如题,我想用java模仿浏览器向服务器发请求
我检测到浏览器是发送类似以下形式的请求:
GET /server.asp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)
Host: 172.16.9.243
Connection: Keep-Alive那么我们能不能用java的Socket给服务器发这样东西并得到返回结果,我写了程序是可以发送的,但是就是得不到返回结果,请问是什么问题。附部分程序(数组reqstr就是上面写的类似浏览器的请求):
Socket s=new java.net.Socket("172.16.9.243",80);
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);for(int i=0;i<reqstr.length;i++)
  out.println(reqstr[i]);
str=in.readLine();
System.out.println(str);

解决方案 »

  1.   

    /**用http方式访问url     * 
         *
         */
        public void  sendurl(String urlstr_send){
            
           
            URL    url = null; 
            try{   
                url = new URL(urlstr_send);
           
                }catch(MalformedURLException e){    
                    System.err.println(e.toString());     
                    System.exit(1);
                    }
                try{           
                    InputStream ins = url.openStream(); 
                    BufferedReader breader = new BufferedReader(new InputStreamReader(ins));       
                     info = breader.readLine(); 
                    System.out.println(info);
                    tf.jTextArea3.append(info+"\n");
      
                    }catch(IOException e){  
                        System.err.println(e.toString()+"发送失败");   
                        System.exit(1);
                        }
            
            
        }
        
        
        /**得到发送返回的值
         * 
         * @return
         */
        public String[] getSendReturn(){
            
            
            
            String returnInfo[] = new String[2];
            returnInfo[0] = info.substring(info.indexOf("dest_no")+9,info.indexOf("state")-2);
            returnInfo[1] = info.substring(info.indexOf("state")+7,info.indexOf("state")+8);
             System.out.println(returnInfo[0]);
             System.out.println(returnInfo[1]);
             tf.jTextArea3.append(returnInfo[0]+"\n");
             tf.jTextArea3.append(returnInfo[1]+"\n");
            return returnInfo;
         }