• Android学习笔记--Http协议


    HttpURLConnection 分别有GET和POST请求
    Post方式的
     1     public void testPOstbendi() throws Exception {
     2         //构建服务器地址
     3         URL url = new URL("http://192.168.40.194/qiantai/admin/login_check.php");
     4         //获取HttpURLConnection对象
     5         HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
     6         //设置请求时间
     7         httpURLConnection.setReadTimeout(10000);
     8         httpURLConnection.setConnectTimeout(10000);
     9         //设置方式
    10         httpURLConnection.setRequestMethod("POST");
    11         //建立一个实际的链接
    12         httpURLConnection.connect();
    13         //获取输出流
    14         OutputStream os = httpURLConnection.getOutputStream();
    15         //POST提交的字段
    16         String message ="username=admin&userpass=123456";
    17         //想服务器写入数据
    18         os.write(message.getBytes());
    19         //获取服务端返回的状态码
    20         int code = httpURLConnection.getResponseCode();
    21         //switch判断一下
    22         switch (code){
    23             case 201:
    24             case 200:
    25                 //获取服务器返回的输入流
    26                 InputStream inputStream = httpURLConnection.getInputStream();
    27                 StringBuffer stringBuffer = new StringBuffer();
    28                 String strings;
    29                 //转换成字符输入流
    30                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    31                 while ((strings = bufferedReader.readLine())!= null){
    32                     stringBuffer.append(strings);
    33                 }
    34                 //关闭数据链接
    35                 httpURLConnection.disconnect();
    36                 Log.e(TAG, "testURl: "+stringBuffer.toString());
    37                 break;
    38             case 401:
    39                 Log.e(TAG, "tes401了啊==========================================================");
    40                 break;
    41         }
    42     }

    GET方式的

        public void testURl() throws Exception {
            //构建URL
            URL url = new URL("http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=15850781443");
            //获取httpURLConnection
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            //设置超市时间
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            //设置请求方式
            httpURLConnection.setRequestMethod("GET");
            //获取请求码
            int code = httpURLConnection.getResponseCode();
            switch (code){
                case 200:
                    InputStream inputStream = httpURLConnection.getInputStream();
                    StringBuffer sb = new StringBuffer();
                    String str;
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                    while ((str = reader.readLine()) != null){
                        sb.append(str+"
    ");
                    }
                    Log.e(TAG, "testURl: "+sb.toString());
                    break;
                case 401:
                    Log.e(TAG, "testURl: 401");
                    break;
            }
        }
     
  • 相关阅读:
    Perl Resources
    使用Perl管理Oracle数据库
    Tomcat 发布部署jsp网站—-使用自己的ip访问jsp网站
    Lode's Computer Graphics Tutorial Image Filtering
    Java Image Filters
    ZPhotoEngine超级算法库
    ps亮度对比度算法
    滤镜艺术---新海诚(你的名字)滤镜特效的实现解密
    Cocoa-GL-Tutorial
    Learning Cocoa with Objective-C
  • 原文地址:https://www.cnblogs.com/langfei8818/p/6053484.html
Copyright © 2020-2023  润新知