httpclient方式提交資料到伺服器

來源:互聯網
上載者:User

標籤:

get方式:
            //使用HttpClient請求伺服器將使用者密碼發送伺服器驗證
                try{
                String path = "http://192.168.13.83:8080/xuexi/servlet/LoginServlet?username="+URLEncoder.encode(username,"utf-8")+"&pwd="+URLEncoder.encode(password,"utf-8");
                //1.建立一個httpClient對象
                HttpClient httpclient = new DefaultHttpClient();
                
                //2.佈建要求的方式
                HttpGet httpget = new HttpGet(path);
                //3.執行一個http請求
                HttpResponse response = httpclient.execute(httpget);
                //4.擷取請求的狀態代碼,
                StatusLine statusLine = response.getStatusLine();
                int code = statusLine.getStatusCode();
                
                //5.判斷狀態代碼後擷取內容
                if(code == 200){
                    HttpEntity entity = response.getEntity();//擷取實體內容,中封裝的有http請求返回的流資訊
                    InputStream inputStream = entity.getContent();
                    //將流資訊轉換成字串
                    String result = StreamUtils.streamToString(inputStream);
                    
                    Message msg = Message.obtain();
                    msg.what = 1;
                    msg.obj = result;
                    handler.sendMessage(msg);
                }
                
                }catch (Exception e) {
                    e.printStackTrace();
                }

 


    post方式:

            //使用UrlConncetion請求伺服器將使用者密碼發送伺服器驗證
                try{
                        String path = "http://192.168.13.83:8080/xuexi/servlet/LoginServlet";
                        //1.建立一個httpclient對象
                        HttpClient httpclient = new DefaultHttpClient();
                        //2.建立一個請求方式
                        HttpPost httppost = new HttpPost(path);
                        //建立集合封裝資料
                        ArrayList<BasicNameValuePair> arrayList = new ArrayList<BasicNameValuePair>();
                        BasicNameValuePair nameValuePair = new BasicNameValuePair("username",username);
                        arrayList.add(nameValuePair);
                        BasicNameValuePair nameValuePair1 = new BasicNameValuePair("pwd",password);
                        arrayList.add(nameValuePair1);
                        
                        //建立一個Entity
                        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(arrayList, "utf-8");
                        //佈建要求時的內容
                        httppost.setEntity(entity);
                        
                        //3.執行一個請求,返回一個response對象
                        HttpResponse response = httpclient.execute(httppost);
                        //4.擷取狀態代碼
                        int code = response.getStatusLine().getStatusCode();
                        //5.判斷並擷取內容
                        if(code == 200){
                            HttpEntity entity1 = response.getEntity();//擷取實體內容,中封裝的有http請求返回的流資訊
                            InputStream inputStream = entity1.getContent();
                            //將流資訊轉換成字串
                            String result = StreamUtils.streamToString(inputStream);
                            Message msg = Message.obtain();
                            msg.what = 2;
                            msg.obj = result;
                            handler.sendMessage(msg);
                        }

                }catch (Exception e) {
                    e.printStackTrace();
                }

httpclient方式提交資料到伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.