论坛首页 Java企业应用论坛

学习Spring必学的Java基础知识(9)----HTTP报文(系列全)

浏览 64982 次
该帖已经被评为精华帖
作者 正文
   发表时间:2012-06-12  
基础知识很重要!
0 请登录后投票
   发表时间:2012-06-12  
哥们,转载一下你的文章可以吧?
0 请登录后投票
   发表时间:2012-06-12  
aq1sw2 写道
哥们,转载一下你的文章可以吧?

欢迎转载,请标注出处。
0 请登录后投票
   发表时间:2012-06-12  
stamen 写道
aq1sw2 写道
哥们,转载一下你的文章可以吧?

欢迎转载,请标注出处。

OK,From:http://www.iteye.com/topic/1124408?page=3 这个了,写的不错,简单明了
0 请登录后投票
   发表时间:2012-06-13  
用非常清楚明白了 的扫我过去的迷糊,谢谢LZ!
0 请登录后投票
   发表时间:2012-06-13  
报文头的属性还可以通过<meta></meta>标签在报文体中定义,希望这个LZ也讲讲,我觉得也挺有用的。
0 请登录后投票
   发表时间:2012-06-13  
好文受教了。
0 请登录后投票
   发表时间:2012-06-16   最后修改:2012-06-16
LZ,问下http的数据包结束标志是什么 ?
很多情况下头部有返回contextLength ,但也有少部分没有的,l = is.read(bys);经常报time out错误,用HttpURLConnection是可以正常访问,我一直找不到HttpURLConnection是怎么处理结束标志的代码。

sun.net.www.http.HttpClient试过,也报time out

/**
     * 访问处理
     * 完整的URL http://www.tianya.cn/focus/index.shtml
     * @param domain 域名,内容为www.tianya.cn
     * @param page 页面,内容为/focus/index.shtml
     * @return 页面的内容
     */
    private ByteArrayOutputStream request(String domain,String page,int port,Map<String, String> pars
    		, boolean isPost, Map<String, String> header)
    	throws UnknownHostException,IOException{

    	InetAddress inet = InetAddress.getByName(domain);
        Socket socket = new Socket(inet.getHostAddress(),port); 
        socket.setSoTimeout(5 * 1000);
        InputStream is = socket.getInputStream();   
        OutputStream os = socket.getOutputStream();  
        try{
            PrintWriter writer = new PrintWriter(os);
            requestWriteHeader(writer, domain, page, port, header,isPost);
            
            ByteArrayOutputStream rs = new ByteArrayOutputStream();
            byte[] bys = new byte[1024 * 32];
            int l = is.read(bys);
            
            StringBuilder contextHeader = new StringBuilder(); 
            int headerlength = parseHeader(bys,l,contextHeader);
            if(headerlength <=0){
                System.err.println("http没有头信息!");
                return rs;
            }
            
            this.charset = getEncode(contextHeader,bys,l);
            int contextLength = getContextLength(contextHeader);
            int size = 0; //防止超时(http响应结束,并不返回-1的)
            do{            	
            	rs.write(bys,headerlength,l);
            	size += l;
            	l = is.read(bys);//经常报time out错误
            }
            while(l!=-1 && (contextLength==0 || size < headerlength + contextLength));

            writer.close();
            return rs;
         }
         finally{
             HttpTools.close(is);
             HttpTools.close(os);
             socket.close();
         }
    }
0 请登录后投票
   发表时间:2012-07-14   最后修改:2012-07-14
netsupper 写道
LZ,问下http的数据包结束标志是什么 ?
很多情况下头部有返回contextLength ,但也有少部分没有的,l = is.read(bys);经常报time out错误,用HttpURLConnection是可以正常访问,我一直找不到HttpURLConnection是怎么处理结束标志的代码。

sun.net.www.http.HttpClient试过,也报time out

/**
     * 访问处理
     * 完整的URL http://www.tianya.cn/focus/index.shtml
     * @param domain 域名,内容为www.tianya.cn
     * @param page 页面,内容为/focus/index.shtml
     * @return 页面的内容
     */
    private ByteArrayOutputStream request(String domain,String page,int port,Map<String, String> pars
    		, boolean isPost, Map<String, String> header)
    	throws UnknownHostException,IOException{

    	InetAddress inet = InetAddress.getByName(domain);
        Socket socket = new Socket(inet.getHostAddress(),port); 
        socket.setSoTimeout(5 * 1000);
        InputStream is = socket.getInputStream();   
        OutputStream os = socket.getOutputStream();  
        try{
            PrintWriter writer = new PrintWriter(os);
            requestWriteHeader(writer, domain, page, port, header,isPost);
            
            ByteArrayOutputStream rs = new ByteArrayOutputStream();
            byte[] bys = new byte[1024 * 32];
            int l = is.read(bys);
            
            StringBuilder contextHeader = new StringBuilder(); 
            int headerlength = parseHeader(bys,l,contextHeader);
            if(headerlength <=0){
                System.err.println("http没有头信息!");
                return rs;
            }
            
            this.charset = getEncode(contextHeader,bys,l);
            int contextLength = getContextLength(contextHeader);
            int size = 0; //防止超时(http响应结束,并不返回-1的)
            do{            	
            	rs.write(bys,headerlength,l);
            	size += l;
            	l = is.read(bys);//经常报time out错误
            }
            while(l!=-1 && (contextLength==0 || size < headerlength + contextLength));

            writer.close();
            return rs;
         }
         finally{
             HttpTools.close(is);
             HttpTools.close(os);
             socket.close();
         }
    }


根据截获的报文,应该是:
<回车>
0
<回车>
<回车>

0 请登录后投票
   发表时间:2012-07-16  
非常诙谐的说。最近正好在SIP中,看完http觉得融汇贯通了。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics