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. php 使用Glob() 查找文件技巧

    定义和用法 glob() 函数返回匹配指定模式的文件名或目录. 该函数返回一个包含有匹配文件 / 目录的数组.如果出错返回 false. 参数 描述 file 必需.规定检索模式. size 可选.规 ...

  2. Web前端:博客美化:三、右上角的Github Ribbon

    1.保存图片到博客园相册 2.复制代码到可以放html代码的地方 我因为数量问题把这段sei到了 页首Html代码 <a href="https://github.com/zhengw ...

  3. Dynamics 365测试和启用邮箱时候一直显示“安排电子邮件配置测试”怎么办?

    摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复284或者20181125可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...

  4. iOS ----------各种判断

    iOS 判断数字 - (BOOL) deptNumInputShouldNumber:(NSString *)str { if (str.length == 0) { return NO; } NSS ...

  5. C#检查字符串是否是合法的HTTP URL地址的代码

    在研发过程,把开发过程较好的一些内容片段记录起来,下面的内容是关于C#检查字符串是否是合法的HTTP URL地址的内容,应该是对各位有较大用处. protected string HTTPChecke ...

  6. Android中将一个图片切割成多个图片

    有种场景,我们想将一个图片切割成多个图片.比如我们在开发一个拼图的游戏,就首先要对图片进行切割. 以下是封装好的两个类,可以实现图片的切割.仅供参考和学习. 一个是ImagePiece类,此类保存了一 ...

  7. Bootstrap-table表格初始化表格数据

    一.项目说明 ①此项目是ASP.NET项目,开发语言是C# ②bootstrap-table使用需要下载对应的css和js插件 ③具体详情还需查看api文档 二.前端代码 <div class= ...

  8. 微信小程序(六) 文章详情静态页面detail

    文章详情静态页面detail:

  9. Linux 配置本地源 (Ubuntu / CentOS)

    目录 Linux local source list A. Ubuntu 1. 本地ISO 2. 制作本地源 B. CentOS 1. 本地ISO Linux local source list A. ...

  10. SSIS的部署和配置

    参考:http://www.cnblogs.com/JasonLiao/p/SSISDeploy.htmlhttps://msdn.microsoft.com/en-us/library/ms1401 ...