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. Mysql 查询练习

    Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...

  2. TAC Alpha版本 冲冲冲!!!

    第1天 第2天 第3天 第4天 第5天 第6天 第7天 第8天 第9天 第10天 测试随笔 冲刺总结

  3. mysql中bit_count和bit_or函数的含义

    翻阅mysql手册时,看到有个示例使用了bit_or方法来去除重复的数据,一开始没看明白,后来看明白之后感觉非常巧妙.示例要实现的功能就是计算每月有几天有访问,先把示例摘录在这里. 1 2 3 4 5 ...

  4. SDL 1.2.15 issue

    SDL 1.2.15中,对于X11的函数,默认采用动态加载的方式 但相应的X11函数名在SDL中并没有重新命名(SDL2中都添加了前缀X11_) 这样在SDL与其他库混合静态编译链接时,X11的函数就 ...

  5. SaaS模式的软件

     SaaS是Software-as-a-Service(软件即服务)的简称,随着互联网技术的发展和应用软件的成熟, 在21世纪开始兴起的一种完全创新的软件应用模式.它与"on-demand  ...

  6. git gui 学习

    目的 自己以前使用过3,4个月的SVN,因为公司使用的是git,git gui.所以打算自学git gui,并记录一下学习心得.^_^ 原因 为什么不是学命令行而是用git gui呢.我觉得首先因为公 ...

  7. C语言基础(8)-const,volatile,register关键字

    1 const const是定义一个常量 2 volatile 代表定义一个变量,这个变量值可能在CPU指令之外被改变 volatile int a;//定义了一个volatile类型的int变量 通 ...

  8. 5Hibernate配置及使用方法----青软S2SH(笔记)

    关于hibernate的简单配置,先看结构图,我们需要 1.还要弄一下需要的 jar包. 2.配置两个文件(hibernate配置文件和映射文件),不过映射文件可以用注解替代. 3.写一个pojo类, ...

  9. AOP基本名词解释

  10. OC中的extern,static,const

    const的作用: const仅仅用来修饰右边的变量(基本数据变量p,指针变量*p). 被const修饰的变量是只读的. static的作用: 修饰局部变量: 1.延长局部变量的生命周期,程序结束才会 ...