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. SSIS同步多个数据库

    这周接到了一个新的需求,从IBM DB2,同步数据到SQLServer.在从SQLServer,同步到Oracle. 因为IBM是32位的平台,ORACLE是64位的平台.而且要求使用计划任务,所以需 ...

  2. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  3. nginx虚拟主机配置小结

    nginx的安装在lnmp环境搭建中已经介绍过了,配置文件在安装目录下的conf子目录下,主要主要分成四部分:main(全局设置).server(主机设置).upstream(负载均衡服务器设置).l ...

  4. 深入理解redis持久化

    持久化方式: 快照(RDB)方式,默认方式,文件以二进制方式保存到RDB文件. 文件追加(AOF)方式,文件以协议文本的方式write到AOF文件. 作用,重启后的数据恢复.当两种方式都启用时,red ...

  5. javascript设置和获取cookie的通用方法

    //获取cookie  function getCookieValue(cookieName)  {     var cookieValue = document.cookie;     var co ...

  6. 《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

  7. PHP函数 addslashes() 和 mysql_real_escape_string() 的区别 && SQL宽字节,绕过单引号注入攻击

    首先:不要使用 mysql_escape_string(),它已被弃用,请使用 mysql_real_escape_string() 代替它. mysql_real_escape_string() 和 ...

  8. ASP.NET Core--基于授权的资源

    翻译如下: 通常授权取决于正在访问的资源. 例如,文档可以具有作者属性. 将只允许文档作者对其进行更新,因此必须在进行授权评估之前从文档存储库加载资源. 这不能使用Authorize属性来完成,因为属 ...

  9. php的register_globals配置

    1.需求 看ci文档的时候,看到register_globals,要了解这个配置的使用 2.分析 register_globals是PHP.ini里的一个配置,这个配置影响到php如何接收传递过来的参 ...

  10. ngCloak 实现 Angular 初始化闪烁最佳实践

    在做angular的SPA开发时,我们经常会遇见在如Chrome这类能够快速解析的浏览器上出现表达式({{ express }} ),或者是模块(div)的闪烁.对于这个问题由于JavaScript去 ...