http请求实质举例

来源:互联网 发布:苹果手机优化软件 编辑:程序博客网 时间:2024/04/29 06:53
一.不带参数的url GET请求.

1.通过浏览器请求
2.通过java 的socket请求,写入字符串与返回字符串
3.通过java 的httpconnection访问url.

辅助工具:tcp/ipmonitor 在1,3 两种请求的时候,分别访问的是8888端口,而不是8080端口.为的是能够记录下请求与放回的数据.

#######################################################################
firefox 请求
http://ld0shqvuyxdpqyd:8888/struts2json/

tcp/ip monitor
request:
-------------------------------
GET /struts2json/ HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Cookie: JSESSIONID=211BD82F577C5E0C0CAB2477537F3B60

response:
-------------------------------
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 628
Date: Tue, 25 Oct 2011 07:39:05 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  略去正常简单的页面内容.    
</html>
#######################################################################
java socket 模拟head请求,代码如下.
public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            Socket ss = new Socket("ld0shqvuyxdpqyd",8080);
            StringBuffer s = new StringBuffer();
            s.append("GET /struts2json/ HTTP/1.1\r\n");
            s.append("Host: ld0shqvuyxdpqyd:8080\r\n");
            s.append("User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\r\n");
            s.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
            s.append("Accept-Language: zh-cn,zh;q=0.5\r\n");
            s.append("Accept-Encoding: gzip, deflate\r\n");
            s.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n");
            s.append("Connection: keep-alive\r\n");
            s.append("Cookie: JSESSIONID=211BD82F577C5E0C0CAB2477537F3B60\r\n");
            s.append("Cache-Control: max-age=0\r\n");
            s.append("\r\n");
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ss.getOutputStream()));
            BufferedReader br = new BufferedReader(new InputStreamReader(ss.getInputStream()));
            bw.write(s.toString());
            bw.flush();
            String mm = "";
            while((mm = br.readLine())!= null){
                System.out.println(mm);    
            }
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            System.out.println("io 没有关闭");
        }
    }

-----------------------------------
打印结果如下:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 634
Date: Tue, 25 Oct 2011 07:57:32 GMT




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 略去正常简单的页面内容.    
</html>
io 没有关闭
#######################################################################
使用java中的httpconnection来实现.
public static void main(String[] args) {
         String urlStr = "http://ld0shqvuyxdpqyd:8888/struts2json/";
         URL url = null;
         HttpURLConnection connection = null;
         try{
            url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();
            
            BufferedReader in = new BufferedReader(new InputStreamReader(connection
                    .getInputStream()));
            System.out.println(connection.getHeaderFields());
            
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                System.out.println(inputLine);
            in.close();
         
         }catch(Exception e){
            
             e.printStackTrace();
         }finally{
             connection.disconnect();
         }
        
    }
monitor监控request:
----------------------------------------------------
GET /struts2json/ HTTP/1.1
User-Agent: Java/1.5.0_19
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive


monitor监控response:
----------------------------------------------------
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C12DC5D9C63A96B00EB3E4150F120A19; Path=/struts2json
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 628
Date: Tue, 25 Oct 2011 08:26:36 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 略去正常简单的页面内容.    
</html>


---------------------------
java 打印结果

{Content-Length=[628], Set-Cookie=[JSESSIONID=C12DC5D9C63A96B00EB3E4150F120A19; Path=/struts2json], Date=[Tue, 25 Oct 2011 08:26:36 GMT], Server=[Apache-Coyote/1.1], Content-Type=[text/html;charset=ISO-8859-1], null=[HTTP/1.1 200 OK]}



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 略去正常简单的页面内容.    
  </body>
</html>
#######################################################################
总结:
1.实质就是socket的链接
2.请求一定有header头,返回有header头,以及返回content内容.
3.请求端没有sessionid时,服务端为其创建.
4.User-Agent类型出了浏览器外,还可以别的方式请求.不过code都是可以模拟用什么浏览器请求.


二.post请求,并且带参数.[不再写所有的实现.仅仅写一下最底层的soket的实现]
-----------------------------------------------------------------------
try {
            Socket ss = new Socket("server",80);
            StringBuffer s = new StringBuffer();
            s.append("POST /test/login!confirm.action HTTP/1.1\r\n");
            s.append("Host: server\r\n");
            s.append("User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1\r\n");
            s.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
            s.append("Accept-Language: zh-cn,zh;q=0.5\r\n");
            s.append("Accept-Encoding: gzip, deflate\r\n");
            s.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n");
            s.append("Connection: keep-alive\r\n");
            s.append("Referer: http://server/test/login.action\r\n");
            s.append("Cookie: JSESSIONID=0000rnMf0ovC30zoOtclZcg7Tld:-1\r\n");
            s.append("Content-Type: application/x-www-form-urlencoded\r\n");
            s.append("Content-Length: 189\r\n");
            s.append("\r\n");
            
            s.append("loginView.ebNoHead=70&loginView.channelType=I&loginView.branchID=812&loginView.loginType=A&loginView.loginIDType=U&gopage=&loginView.loginID=testloginout&loginView.password=111111&x=35&y=11\r\n");
            
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ss.getOutputStream()));
            BufferedReader br = new BufferedReader(new InputStreamReader(ss.getInputStream()));
            bw.write(s.toString());
            bw.flush();
            String mm = "";
            while((mm = br.readLine())!= null){
                System.out.println(mm);    
            }
            
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            System.out.println("io 没有关闭");
        }

看到仍然是需要,header头,只不过在header结束的地方,加入了请求的参数及值.


原创粉丝点击