import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map; /**
* HTTP请求类
* @author
*/
public class HttpInvoker { /**
* GET请求
* @param getUrl
* @throws IOException
* @return 提取HTTP响应报文包体,以字符串形式返回
*/
public static String httpGet(String getUrl) throws IOException {
//1 把字符串形式的网址转成 java.net.URL对象
URL getURL = new URL(getUrl);
/*
* 2 通过java.net.URL打开一个连接(获取一个代表与远程对象的URLConnection对象),并转为 HttpURLConnection ,
Returns a URLConnection object that represents a connection
to the remote object referred to by the URL.
*/
HttpURLConnection connection = (HttpURLConnection) getURL.openConnection();
//3 通过HttpURLConnection对象真正获取连接
connection.connect();
//4 连接后那么这个远程连接对象就可以通过自身的一些方法读取到连接的信息了,这里是把流转成字符串
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sbStr = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sbStr.append(line);
}
bufferedReader.close();
connection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
} public static void main(String[] args) throws IOException {
String url = "https://www.hao123.com/?tn=99682755_hao_pg";
String rtn_json = httpGet(url);
System.out.println(""+rtn_json);
} /**
* POST请求
* @param postUrl
* @param postHeaders
* @param postEntity
* @throws IOException
* @return 提取HTTP响应报文包体,以字符串形式返回
*/
public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException { URL postURL = new URL(postUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.setRequestProperty(" Content-Type ", " application/x-www-form-urlencoded "); if(postHeaders != null) {
for(String pKey : postHeaders.keySet()) {
httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));
}
}
if(postEntity != null) {
DataOutputStream out = new DataOutputStream(httpURLConnection.getOutputStream());
out.writeBytes(postEntity);
out.flush();
out.close(); // flush and close
}
//connection.connect();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder sbStr = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sbStr.append(line);
}
bufferedReader.close();
httpURLConnection.disconnect();
return new String(sbStr.toString().getBytes(),"utf-8");
} }

HttpInvoker http请求工具类的更多相关文章

  1. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

  2. Http、Https请求工具类

    最近在做微信开发,使用http调用第三方服务API,有些是需要https协议,通过资料和自己编码,写了个支持http和https的工具类,经验证可用,现贴出来保留,也供需要的人使用(有不足的地方,也请 ...

  3. 微信https请求工具类

    工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...

  4. HTTP请求工具类

    HTTP请求工具类,适用于微信服务器请求,可以自测 代码; /// <summary> /// HTTP请求工具类 /// </summary> public class Ht ...

  5. 实现一个简单的http请求工具类

    OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...

  6. 远程Get,Post请求工具类

    1.远程请求工具类   import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.L ...

  7. C#实现的UDP收发请求工具类实例

    本文实例讲述了C#实现的UDP收发请求工具类.分享给大家供大家参考,具体如下: 初始化: ListeningPort = int.Parse(ConfigurationManager.AppSetti ...

  8. ajax请求工具类

    ajax的get和post请求工具类: /** * 公共方法类 *  * 使用  变量名=function()定义函数时,如果在变量名前加var,则这个变量变成局部变量 */var Common = ...

  9. 【原创】标准HTTP请求工具类

    以下是个人在项目开发过程中,总结的Http请求工具类,主要包括四种: 1.处理http POST请求[XML格式.无解压]: 2.处理http GET请求[XML格式.无解压]: 3.处理http P ...

随机推荐

  1. tf.nn.conv2d。卷积函数

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,参考文档对它的介绍并不是很详细,实际上这是搭建卷积神经网络比较核心的一个方法,非常重要 tf.nn.conv2d(input, fil ...

  2. append和appendTo的区别!

    今天在写dome的时候,碰到了一小点问题,就是我们想把一个小效果用jquery的办法添加到HTML页面中.我用的办法就是先在HTML中把代码写完,js和css同样写好并调试完成后.然后只保存外面最大的 ...

  3. arcgis api 3.x for js 共享干货系列之二自定义 Navigation 控件样式风格(附源码下载)

    0.内容概览 自定义 Navigation 控件样式风格 源码下载 1.内容讲解 arcgis api 3.x for js 默认的Navigation控件样式风格如下图:这样的风格不能说不好,各有各 ...

  4. JavaScript中的闭包和作用域链

    这部分几乎是JavaScript中最难的部分,也是面试官最爱问的地方. 下面的内容是我以前写的<JavaScript学习手册>中被客户删除的部分,理由听起来有点诡异:太难.

  5. 微信小程序转发微信小程序转发

    微信小程序转发涉及以下4个方法: 1.Page.onShareAppMessage({}) 设置右上角“转发”配置,及转发后回调函数返回 shareTicket 票据 2.wx.showSahreMe ...

  6. [翻译]:MySQL Error: Too many connections

    翻译:MySQL Error: Too many connections   前言: 本文是对Muhammad Irfan的这篇博客MySQL Error: Too many connections的 ...

  7. webmagic 爬取网页所有文章的标题时间作者和内容

    package com.ij34; import us.codecraft.webmagic.Site; import us.codecraft.webmagic.Page; import us.co ...

  8. 无法确定条件表达式的类型,因为“<null>”和“System.DateTime”之间没有隐式转换----解决办法

    例子:(报错了) public DateTime? time { get; set; } time = item.HospOutDate.HasValue ? DateTime.Parse(item. ...

  9. Integer a= 127 与 Integer b = 128相关

    Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; a == b 与 c == d 的比较结果是什么? a == b ...

  10. Linux Mysql数据库安全配置

    Linux  Mysql数据库安全配置 目录: 1.修改mysql管理员账号root的密码(2种方法) 2.修改mysql管理员账号root 3.mysql管理员root账号密码遗忘解决办法(2种方法 ...