httpclient方式提交数据到服务器

get方式:
        //使用HttpClient请求服务器将用户密码发送服务器验证
            try{
            String path = "http://192.168.13.83:8080//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/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();
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值