zoukankan      html  css  js  c++  java
  • Android HTTP请求 POST、GET

    //code by:博客园-曹永思

      Android HTTP请求 POST、GET
        //HTTP 请求 GET方式
        public String GetHttpGet() {
            String result = null;
            URL url = null;
            HttpURLConnection connection = null;
            InputStreamReader in = null;
            try {
                url = new URL("http://192.168.1.202:803/t.ashx?id=sdaf");
                connection = (HttpURLConnection) url.openConnection();
                in = new InputStreamReader(connection.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(in);
                StringBuffer strBuffer = new StringBuffer();
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    strBuffer.append(line);
                }
                result = strBuffer.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
            }
            return result;
        }
    
     
    
        //HTTP 请求 POST方式
        private String GetHttpPost() {
            String r = "";
            try {
                final String SERVER_URL = "http://192.168.1.202:803/t.ashx"; // 定义需要获取的内容来源地址
                HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求
                List params = new ArrayList();
                params.add(new BasicNameValuePair("id", "London")); // 添加必须的参数
                request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置参数的编码
                HttpResponse httpResponse = new DefaultHttpClient()
                        .execute(request); // 发送请求并获取反馈
                // 解析返回的内容
                if (httpResponse.getStatusLine().getStatusCode() != 404) {
                    String result = EntityUtils.toString(httpResponse.getEntity());
                    // System.out.println(result);
                    r = result;
                }
            } catch (Exception e) {
            }
            return r;
        }
    
     .net重写URL:http://www.cnblogs.com/yonsy/archive/2012/09/21/2696935.html

     欢迎转载,转载请注明出处,希望帮到更多人。

  • 相关阅读:
    [freemarker篇]03.如何处理空值
    [Android篇]Android Studio + Genymotion 一夜无眠 ,超级详细版本[请使用新版2.0]
    [freemarker篇]02.生成HTML的静态页面
    [freemarker篇]01.入门Freemarker示例
    验证码-直接使用
    jquery基础介绍-转
    VBA与宏
    .net打印
    [CCF] 201612-1 中间数
    [LeetCode] 56. Merge Intervals(vector sort)
  • 原文地址:https://www.cnblogs.com/yonsy/p/3089052.html
Copyright © 2011-2022 走看看