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

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

  • 相关阅读:
    asp.net 页面元素分析搜集
    ASP.NET AJAX深入浅出系列UpdatePanel的使用笔记(上)
    用sql语句来管理数据库日志问题
    C# .NET学习网站(转)
    Visual Studio .Net团队开发[转]
    sql 语句大全
    Word中快速操作的10个技巧
    嫁给程序员的好处
    关于手机病毒时代到来的担忧
    自己工作用过的SQL代码(1)
  • 原文地址:https://www.cnblogs.com/yonsy/p/3089052.html
Copyright © 2011-2022 走看看