package com.baqingshe.bjs.util;

 import java.io.BufferedReader;

 import java.io.IOException;

 import java.io.InputStream;

 import java.io.InputStreamReader;

 import java.io.PrintWriter;

 import java.net.URL;

 import java.net.URLConnection;

 import java.util.List;

 import java.util.Map;

 import sun.net.www.protocol.http.HttpURLConnection;

 public class HttpRequest {

   public static String doGet(String url) throws Exception {

        URL localURL = new URL(url);

        URLConnection connection = localURL.openConnection();

        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;

        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");

        httpURLConnection.setRequestProperty("Content-Type", "application/json");

        InputStream inputStream = null;

        InputStreamReader inputStreamReader = null;

        BufferedReader reader = null;

        StringBuffer resultBuffer = new StringBuffer();

        String tempLine = null;

        if (httpURLConnection.getResponseCode() >= 300) {

            throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());

        }

        try {

            inputStream = httpURLConnection.getInputStream();

            inputStreamReader = new InputStreamReader(inputStream);

            reader = new BufferedReader(inputStreamReader);

            while ((tempLine = reader.readLine()) != null) {

                resultBuffer.append(tempLine);

            }

        } finally {

            if (reader != null) {

                reader.close();

            }

            if (inputStreamReader != null) {

                inputStreamReader.close();

            }

            if (inputStream != null) {

                inputStream.close();

            }

        }

        return resultBuffer.toString();

    }

     public static String sendPost(String url, String param) {

         PrintWriter out = null;

         BufferedReader in = null;

         String result = "";

         try {

             URL realUrl = new URL(url);

             // 打开和URL之间的连接

             URLConnection conn = realUrl.openConnection();

             // 设置通用的请求属性

             conn.setRequestProperty("accept", "*/*");

             conn.setRequestProperty("connection", "Keep-Alive");

             conn.setRequestProperty("user-agent",

                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

             // 发送POST请求必须设置如下两行

             conn.setDoOutput(true);

             conn.setDoInput(true);

             // 获取URLConnection对象对应的输出流

             out = new PrintWriter(conn.getOutputStream());

             // 发送请求参数

             out.print(param);

             // flush输出流的缓冲

             out.flush();

             // 定义BufferedReader输入流来读取URL的响应

             in = new BufferedReader(

                     new InputStreamReader(conn.getInputStream()));

             String line;

             while ((line = in.readLine()) != null) {

                 result += line;

             }

         } catch (Exception e) {

             System.out.println("发送 POST 请求出现异常!"+e);

             e.printStackTrace();

         }

         //使用finally块来关闭输出流、输入流

         finally{

             try{

                 if(out!=null){

                     out.close();

                 }

                 if(in!=null){

                     in.close();

                 }

             }

             catch(IOException ex){

                 ex.printStackTrace();

             }

         }

         return result;

     }    

 }

java发送GET和post请求的更多相关文章

  1. Java发送get及post请求工具方法

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  2. JAVA发送http get/post请求,调用http接口、方法

    import java.io.BufferedReader; import java.io.IOException;import java.io.InputStream; import java.io ...

  3. Java发送http get/post请求,调用接口/方法

    由于项目中要用,所以找了一些资料,整理下来. GitHub地址: https://github.com/iamyong    转自:http://blog.csdn.net/capmiachael/a ...

  4. JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理

    java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...

  5. Java 发送Get和Post请求

    package com.htpt.superviseServices.dm.util; import java.io.BufferedReader; import java.io.IOExceptio ...

  6. Java 发送http GET/POST请求

    最近项目里面需要用到Java发送http请求,由于发送https请求有点复杂,暂时不考虑 HttpURLConnection HttpURLConnection是一种多用途.轻量极的HTTP客户端,使 ...

  7. 如何用java发送Http的post请求,并传递参数

    书写方法,请参考以下代码: package utils; import java.io.BufferedReader; import java.io.IOException; import java. ...

  8. java发送post 的json请求

    package com.elink.estos.mq.mqmanager; import java.io.IOException; import java.io.InputStream; import ...

  9. 【工具】java发送GET、POST请求

    前项目使用这种HTTP的方式进行数据交互,目前已更换数据交互方式,但是作为接口提供调用来说还是比较简洁高效的: 总体流程就是: 1.发送HTTP请求 2.获取返回的JSON对象 3.JSON转换 pa ...

随机推荐

  1. oneuijs/You-Dont-Need-jQuery

    oneuijs/You-Dont-Need-jQuery  https://github.com/oneuijs/You-Dont-Need-jQuery/blob/master/README.zh- ...

  2. Nginx简易配置文件(三)(文件缓存)

    server { listen 80; listen 443 ssl; server_name user.17.net userapi.17.net; access_log logs/user/acc ...

  3. 30秒懂SQL中的join(2幅图+30秒)

    废话不多说,直接上图秒懂. t1表的结构与数据如下: t2表的结构与数据如下: inner join select * from t1 inner join t2 on t1.id = t2.id; ...

  4. php无限分类

    无限循环 1.需要套2个foreach 2.2个foreach结构一样 纯代码获取数据 ){ $where['parent_id']= $parent_id; $res = $this->m-& ...

  5. FMDB的使用

    //1.创建数据库    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomai ...

  6. CSS样式自动换行(强制换行)与强制不换行

    自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换行的方法 对于div,p等块级元素,正常文字的换行(亚洲文字和非亚洲文字)元素拥 ...

  7. C和指针 第七章 习题

    7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...

  8. ICP 算法

    ICP 算法是一种点云到点云的配准方法. 在SLAM中通过空间点云的配准(可以通过相机或者3D激光雷达获取点云数据),可以估计相机运动(机器人运动,旋转矩阵R与平移向量t),累积配准,并不断回环检测, ...

  9. Windows7 + Ubuntu双系统安装过程记录

    本文为在已安装Windows7系统的前提下安装Ubuntu Kylin 14.10系统的过程以及期间出现的各种问题的记录. Ubuntu系统下载 Ubuntu Kylin中文官方网站:http://w ...

  10. C#高级编程笔记 Day 8, 2016年9月 28日 (数组)

    1.数组的初始化 声明了数组后,就必须为数组分配内存,以保存数组的所有元素.数组是引用类型,所以必须给它分配堆上的内存,为此,应使用 new 运算符,指定数组中元素的类型和数量来初始化数组的变量.若使 ...