`
leiyongping88
  • 浏览: 75954 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Java与Http协议(HttpURLConnection和HttpClient)

 
阅读更多

转载:http://www.csdn123.com/html/blogs/20130505/9360.htm

 

引言                                        

      http(超文本传输协议)是一个基于请求与响应模式的、无状态的、应用层的协议,常基于TCP的连接方式。HTTP协议的主要特点是:
     1.支持客户/服务器模式。
     2.简单快速:客户向服务器请求服务时,只需传送请求方法和路径。由于HTTP协议简单,通信速度很快。
     3.灵活:HTTP允许传输任意类型的数据对象。类型由Content-Type加以标记。
     4.无连接:即每次连接只处理一个请求,处理完客户的请求,并收到客户的应答后,即断开连接。采用这种方式可以节省传输时间。
     5.无状态:无状态是指协议对于事务处理没有记忆能力。 

     http1.0协议默认的是非持久连接, HTTP1.1默认的连接方式为持久连接。

非持久连接:每次服务器发出一个对象后,相应的TCP连接就被关闭,也就是说每个连接都没有持续到可用于传送其他对象。每个TCP连接只用于传输一个请求消息和一个响应消息。

持久连接:服务器在发出响应后让TCP连接继续打开着。同一对客户/服务器之间的后续请求和响应可以通过这个连接发送。HTTP/1.1的默认模式使用带流水线的持久连接。

 

一、HTTP协议详解之请求

//请求行
POST /reg.jsp HTTP/ (CRLF)       

//消息报头
Accept:image/gif,image/x-xbitmap,image/jpeg,application/x-shockwave-flash,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,*/* (CRLF)
Accept-Language:zh-cn (CRLF)
Accept-Encoding:gzip,deflate (CRLF)
If-Modified-Since:Wed,05 Jan 2007 11:21:25 GMT (CRLF)
If-None-Match:W/"80b1a4c018f3c41:8317" (CRLF)
User-Agent:Mozilla/4.0(compatible;MSIE6.0;Windows NT 5.0) (CRLF)
Host:www.guet.edu.cn (CRLF)
Connection:Keep-Alive (CRLF)
(CRLF)

//请求正文
user=jeffrey&pwd=1234


以上是http请求的三部:请求行、消息报头、请求正文。

 

     请求行以一个方法符号开头,以空格分开,后面跟着请求的URI和协议的版本,格式如下:

Method Request-URI HTTP-Version CRLF  
     其中 Method表示请求方法(如POST、GET、PUT、DELETE等);Request-URI是一个统一资源标识符;HTTP-Version表示请求的HTTP协议版本;CRLF表示回车和换行。

 

二、HTTP协议详解之响应篇

//状态行
HTTP/1.1 200 OK (CRLF)

//消息报头
Cache-Control: private, max-age=30
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Mon, 25 May 2009 03:20:33 GMT
Last-Modified: Mon, 25 May 2009 03:20:03 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Mon, 25 May 2009 03:20:02 GMT
Content-Length: 12173

//响应正文
略


HTTP响应也是由三个部分组成,分别是:状态行、消息报头、响应正文

     状态行格式如下:
          HTTP-Version Status-Code Reason-Phrase CRLF
     其中,HTTP-Version表示服务器HTTP协议的版本;Status-Code表示服务器发回的响应状态代码;Reason-Phrase表示状态代码的文本描述。

常见状态代码、状态描述、说明:
200 OK      //客户端请求成功
400 Bad Request  //客户端请求有语法错误,不能被服务器所理解
401 Unauthorized //请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用 
403 Forbidden  //服务器收到请求,但是拒绝提供服务
404 Not Found  //请求资源不存在,eg:输入了错误的URL
500 Internal Server Error //服务器发生不可预期的错误
503 Server Unavailable  //服务器当前不能处理客户端的请求,一段时间后可能恢复正常

 

三、HTTP协议详解之消息报头

        HTTP消息由客户端到服务器的请求和服务器到客户端的响应组成。请求消息和响应消息都是由开始行(对于请求消息,开始行就是请求行;对于响应消息,开始行就是状态行),消息报头(可选),空行(只有CRLF的行),消息正文(可选)组成。

        HTTP消息报头包括普通报头、请求报头、响应报头、实体报头。每一个报头域都是由名字+“:”+空格+值 组成,消息报头域的名字是大小写无关的。

1、请求报头
     请求报头允许客户端向服务器端传递请求的附加信息以及客户端自身的信息。


常用的请求报头

Accept请求报头域用于指定客户端接受哪些类型的信息。
Accept-Charset请求报头域用于指定客户端接受的字符集。
Accept-Encoding请求报头域类似于Accept,但是它是用于指定可接受的内容编码。
Accept-Language请求报头域类似于Accept,但是它是用于指定一种自然语言。
Authorization请求报头域主要用于证明客户端有权查看某个资源。

Host请求报头域主要用于指定被请求资源的Internet主机和端口号,它通常从HTTP URL中提取出来的。User-Agent请求报头域允许客户端将它的操作系统、浏览器和其它属性告诉服务器。

2、响应报头

     响应报头允许服务器传递不能放在状态行中的附加响应信息,以及关于服务器的信息和对Request-URI所标识的资源进行下一步访问的信息。


常用的响应报头
Location响应报头域用于重定向接受者到一个新的位置。Location响应报头域常用在更换域名的时候。
Server响应报头域包含了服务器用来处理请求的软件信息

3. 实体报头

请求和响应消息都可以传送一个实体。


常用的实体报头
Content-Encoding指示已经被应用到实体正文的附加内容的编码。

Content-Language实体报头域描述了资源所用的自然语言。

Content-Length实体报头域用于指明实体正文的长度,以字节方式存储的十进制数字来表示。
Content-Type实体报头域用语指明发送给接收者的实体正文的媒体类型。
Last-Modified实体报头域用于指示资源的最后修改日期和时间。
Expires实体报头域给出响应过期的日期和时间。

 

四、补充

1、HTTP协议Content Lenth限制漏洞导致拒绝服务攻击
使用POST方法时,可以设置ContentLenth来定义需要传送的数据长度,例如ContentLenth:999999999,在传送完成前,内 存不会释放,攻击者可以利用这个缺陷,连续向WEB服务器发送垃圾数据直至WEB服务器内存耗尽。这种攻击方法基本不会留下痕迹。
2、为了提高用户使用浏览器时的性能,现代浏览器还支持并发的访问方式,浏览一个网页时同时建立多个连接,以迅速获得一个网页上的多个图标,这样能更快速 完成整个网页的传输。HTTP1.1中提供了这种持续连接的方式,而下一代HTTP协议:HTTP-NG更增加了有关会话控制、丰富的内容协商等方式的支 持,来提供更高效率的连接。

 

五.Java利用HTTP协议实现联网和下载

Url的请求连接(Get方式)

String currentUrl=“http://www.myWeb.com/login.jsp?userName='Devin'&passWord='mypassword'”; //URL ?后面的内容为HTTP请求的正文URL url = new URL(currentUrl);
 
HttpURLConnection httpurlconnection = url.openConnection();//下面的设置对应HTTP请求中的消息报头
httpurlconnection.setRequestProperty("User-Agent",CommonValues.User_Agent);
httpurlconnection.setRequestProperty("Accept",CommonValues.Accept);
httpurlconnection.setRequestProperty("Accept-Charset",CommonValues.Accept_Charset);
httpurlconnection.setRequestProperty("Accept-Language",CommonValues.Accept_Language);
httpurlconnection.setRequestProperty("Connection",CommonValues.Connection);
httpurlconnection.setRequestProperty("Keep-Alive",CommonValues.Keep_Alive);
httpurlconnection.setConnectTimeout(CommonValues.ConnectionTimeOut);
httpurlconnection.setReadTimeout(CommonValues.ReadTimeOut);
             
httpurlconnection.connect();
            
int responsecode = httpurlconnection.getResponseCode();
             
if(responsecode == HttpURLConnection.HTTP_OK) //对应HTTP响应中状态行的响应码{
  //操作请求流,这里对应HTTP响应中的响应正文
}
             
if (httpurlconnection != null) 
{
   httpurlconnection.disconnect();
}


六、HttpURLConnection和HttpClient 

1.概念      

      HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能:HttpURLConnection。但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。

      除此之外,在Android中,androidSDK中集成了ApacheHttpClient模块,用来提供高效的、最新的、功能丰富的支持 HTTP 协议工具包,并且它支持 HTTP 协议最新的版本和建议。使用HttpClient可以快速开发出功能强大的Http程序。

2.区别

HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等,

HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便,比如重访问的自定义,以及一些高级功能等。

 

 

URLConnection

HTTPClient

Proxies and SOCKS

Authorization

Methods

Headers

Automatic Redirection Handling

Persistent Connections

Pipelining of Requests

Can handle protocols other than HTTP

Can do HTTP over SSL (https)

Source code available

Full support in Netscape browser, appletviewer, and applications (SOCKS: Version 4 only); no additional limitations from security policies.

Full support (SOCKS: Version 4 and 5); limited in applets however by security policies; in Netscape can't pick up the settings from the browser.

Full support for Basic Authorization in Netscape (can use info given by the user for normal accesses outside of the applet); no support in appletviewer or applications.

Full support everywhere; however cannot access previously given info from Netscape, thereby possibly requesting the user to enter info (s)he has already given for a previous access. Also, you can add/implement additional authentication mechanisms yourself.

Only has GET and POST.

Has HEAD, GET, POST, PUT, DELETE, TRACE and OPTIONS, plus any arbitrary method.

Currently you can only set any request headers if you are doing a POST under Netscape; for GETs and the JDK you can't set any headers. 
Under Netscape 3.0 you can read headers only if the resource was returned with a Content-length header; if no Content-length header was returned, or under previous versions of Netscape, or using the JDK no headers can be read.

Allows any arbitrary headers to be sent and received.

Yes.

Yes (as allowed by the HTTP/1.1 spec).

No support currently in JDK; under Netscape uses HTTP/1.0 Keep-Alive's.

Supports HTTP/1.0 Keep-Alive's and HTTP/1.1 persistence.

No.

Yes.

Theoretically; however only http is currently implemented.

No.

Under Netscape, yes. Using Appletviewer or in an application, no.

No (not yet).

No.

Yes.

3.案例

URLConnection

String urlAddress = "http://192.168.1.102:8080/AndroidServer/login.do";  
    URL url;  
    HttpURLConnection uRLConnection;  
    public UrlConnectionToServer(){  
  
    }      //向服务器发送get请求
    public String doGet(String username,String password){  
        String getUrl = urlAddress + "?username="+username+"&password="+password;  
        try {  
            url = new URL(getUrl);  
            uRLConnection = (HttpURLConnection)url.openConnection();  
            InputStream is = uRLConnection.getInputStream();  
            BufferedReader br = new BufferedReader(new InputStreamReader(is));  
            String response = "";  
            String readLine = null;  
            while((readLine =br.readLine()) != null){  
                //response = br.readLine();  
                response = response + readLine;  
            }  
            is.close();  
            br.close();  
            uRLConnection.disconnect();  
            return response;  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
            return null;  
        } catch (IOException e) {  
            e.printStackTrace();  
            return null;  
        }  
    }  
          //向服务器发送post请求
    public String doPost(String username,String password){  
        try {  
            url = new URL(urlAddress);  
            uRLConnection = (HttpURLConnection)url.openConnection();  
            uRLConnection.setDoInput(true);  
            uRLConnection.setDoOutput(true);  
            uRLConnection.setRequestMethod("POST");  
            uRLConnection.setUseCaches(false);  
            uRLConnection.setInstanceFollowRedirects(false);  
            uRLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
            uRLConnection.connect();  
              
            DataOutputStream out = new DataOutputStream(uRLConnection.getOutputStream());  
            String content = "username="+username+"&password="+password;  
            out.writeBytes(content);  
            out.flush();  
            out.close();  
              
            InputStream is = uRLConnection.getInputStream();  
            BufferedReader br = new BufferedReader(new InputStreamReader(is));  
            String response = "";  
            String readLine = null;  
            while((readLine =br.readLine()) != null){  
                //response = br.readLine();  
                response = response + readLine;  
            }  
            is.close();  
            br.close();  
            uRLConnection.disconnect();  
            return response;  
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
            return null;  
        } catch (IOException e) {  
            e.printStackTrace();  
            return null;  
        }  
    }


 

HTTPClient

String urlAddress = "http://192.168.1.102:8080/qualityserver/login.do";  
public HttpClientServer(){  
          
 }  
      
public String doGet(String username,String password){  
    String getUrl = urlAddress + "?username="+username+"&password="+password;  
    HttpGet httpGet = new HttpGet(getUrl);  
    HttpParams hp = httpGet.getParams();  
    hp.getParameter("true");  
    //hp.  
    //httpGet.setp  
    HttpClient hc = new DefaultHttpClient();  
    try {  
        HttpResponse ht = hc.execute(httpGet);  
        if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
            HttpEntity he = ht.getEntity();  
            InputStream is = he.getContent();  
            BufferedReader br = new BufferedReader(new InputStreamReader(is));  
            String response = "";  
            String readLine = null;  
            while((readLine =br.readLine()) != null){  
                //response = br.readLine();  
                response = response + readLine;  
            }  
            is.close();  
            br.close();  
              
            //String str = EntityUtils.toString(he);  
            System.out.println("========="+response);  
            return response;  
        }else{  
            return "error";  
        }  
    } catch (ClientProtocolException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
        return "exception";  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
        return "exception";  
    }      
}  
  
public String doPost(String username,String password){  
    //String getUrl = urlAddress + "?username="+username+"&password="+password;  
    HttpPost httpPost = new HttpPost(urlAddress);  
    List params = new ArrayList();  
    NameValuePair pair1 = new BasicNameValuePair("username", username);  
    NameValuePair pair2 = new BasicNameValuePair("password", password);  
    params.add(pair1);  
    params.add(pair2);  
      
    HttpEntity he;  
    try {  
        he = new UrlEncodedFormEntity(params, "gbk");  
        httpPost.setEntity(he);  
          
    } catch (UnsupportedEncodingException e1) {  
        // TODO Auto-generated catch block  
        e1.printStackTrace();  
    }   
      
    HttpClient hc = new DefaultHttpClient();  
    try {  
        HttpResponse ht = hc.execute(httpPost);  
        //连接成功  
        if(ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
            HttpEntity het = ht.getEntity();  
            InputStream is = het.getContent();  
            BufferedReader br = new BufferedReader(new InputStreamReader(is));  
            String response = "";  
            String readLine = null;  
            while((readLine =br.readLine()) != null){  
                //response = br.readLine();  
                response = response + readLine;  
            }  
            is.close();  
            br.close();  
              
            //String str = EntityUtils.toString(he);  
            System.out.println("=========&&"+response);  
            return response;  
        }else{  
            return "error";  
        }  
    } catch (ClientProtocolException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
        return "exception";  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
        return "exception";  
    }     
}


 

servlet端json转化: 

resp.setContentType("text/json");  
        resp.setCharacterEncoding("UTF-8");  
        toDo = new ToDo();  
        List<UserBean> list = new ArrayList<UserBean>();  
        list = toDo.queryUsers(mySession);  
        String body;  

        //设定JSON  
        JSONArray array = new JSONArray();  
        for(UserBean bean : list)  
        {  
            JSONObject obj = new JSONObject();  
            try  
            {  
                 obj.put("username", bean.getUserName());  
                 obj.put("password", bean.getPassWord());  
             }catch(Exception e){}  
             array.add(obj);  
        }  
        pw.write(array.toString());  
        System.out.println(array.toString());


接收端:

String urlAddress = "http://192.168.1.102:8080/qualityserver/result.do";  
        String body =   
            getContent(urlAddress);  
        JSONArray array = new JSONArray(body);            
        for(int i=0;i<array.length();i++)  
        {  
            obj = array.getJSONObject(i);  
            sb.append("用户名:").append(obj.getString("username")).append("\t");  
            sb.append("密码:").append(obj.getString("password")).append("\n");  
              
            HashMap<String, Object> map = new HashMap<String, Object>();  
            try {  
                userName = obj.getString("username");  
                passWord = obj.getString("password");  
            } catch (JSONException e) {  
                e.printStackTrace();  
            }  
            map.put("username", userName);  
            map.put("password", passWord);  
            listItem.add(map);  
              
        }  
          
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
        if(sb!=null)  
        {  
            showResult.setText("用户名和密码信息:");  
            showResult.setTextSize(20);  
        } else  
            extracted();  
   
       //设置adapter   
        SimpleAdapter simple = new SimpleAdapter(this,listItem,  
                android.R.layout.simple_list_item_2,  
                new String[]{"username","password"},  
                new int[]{android.R.id.text1,android.R.id.text2});  
        listResult.setAdapter(simple);  
          
        listResult.setOnItemClickListener(new OnItemClickListener() {  
            @Override  
            public void onItemClick(AdapterView<?> parent, View view,  
                    int position, long id) {  
                int positionId = (int) (id+1);  
                Toast.makeText(MainActivity.this, "ID:"+positionId, Toast.LENGTH_LONG).show();  
              
            }  
        });  
    }  
    private void extracted() {  
        showResult.setText("没有有效的数据!");  
    }  
    //和服务器连接  
    private String getContent(String url)throws Exception{  
        StringBuilder sb = new StringBuilder();  
        HttpClient client =new DefaultHttpClient();  
        HttpParams httpParams =client.getParams();  
          
        HttpConnectionParams.setConnectionTimeout(httpParams, 3000);  
        HttpConnectionParams.setSoTimeout(httpParams, 5000);  
        HttpResponse response = client.execute(new HttpGet(url));  
        HttpEntity entity =response.getEntity();  
          
        if(entity !=null){  
            BufferedReader reader = new BufferedReader(new InputStreamReader  
                    (entity.getContent(),"UTF-8"),8192);  
            String line =null;  
            while ((line= reader.readLine())!=null){  
                sb.append(line +"\n");  
            }  
            reader.close();  
        }  
        return sb.toString();  
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics