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. 开源IM项目-InChat登录接口设计与实现(基于Netty)

  2. .NET MVC 简单的插件式开发

    插件式开发的优势 1.提高软件的复用度 2.提高软件开发的并行性 3.缩短软件的研发周期.节约研发成本,带给程序开发人员更多的灵活性,产品在软件发布以后还可以添加新的插件和完善已有的功能. 4.方便软 ...

  3. 用python爬取杭电oj的数据

    暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...

  4. 跨域iframe如何实现高度自适应?

    经常有项目会要求实现iframe高度自适应,如果是同域的还好说,如果是跨域的,父页面没有办法操作子页面,想要正确获取子页面高度的话,可以采用以下办法: 方法一:使用HTML5 postMessage ...

  5. 通过Web API调用Action时各种类型输入参数传递值的方法

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

  6. Python+ITchart实现微信中男女比例,城市分布统计并可视化显示

    直接上代码: import itchat import os import csv import pandas as pd from pyecharts import Bar,Pie,Geo impo ...

  7. js调用浏览器打印指定div内容

    --打印按钮事件 function printForm(){    var headstr = '<html xmlns:th="http://www.thymeleaf.org&qu ...

  8. 用idhttp打开网页或下载文件时如何显示进度

    在它的workbegin work事件中写代码 procedure TfrmDownLoad.IdHTTP1WorkBegin(Sender: TObject;   AWorkMode: TWorkM ...

  9. C#常用的网络组件

    常用的网络组件 using System.Net;//为多种网络协议提供统一和简单的编程接口 using System.Net.Mail;//为简单邮件传输协议的服务器提供E-mail发送的类 usi ...

  10. MySQL常用字符串函数

    字符串函数 是最常用的的一种函数,在一个具体应用中通常会综合几个甚至几类函数来实现相应的应用: 1.LOWER(column|str):将字符串参数值转换为全小写字母后返回 mysql> sel ...