HTTP请求代理类(GET 、 POST 、PUT 、DELETE)
package com.jm.http.tools; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL; import org.apache.log4j.Logger; /**
* HTTP请求代理类
*
* @author hsw
* @version 1.0, 2007-7-3
*/
public class UrlOpreate { private static Logger logger = Logger.getLogger(UrlOpreate.class); /**
* POST
**/
public static String sendPostRequest(String requestUrl, String payload) {
StringBuffer jsonString = new StringBuffer();
HttpURLConnection connection=null;
BufferedReader br=null;
try {
URL url = new URL(requestUrl);
connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST"); connection.setRequestProperty("user-agent", "Mozilla/5.0 (compatible; MSIE 11.0; Windows NT 6.1; Trident/5.0)");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setReadTimeout(300000);
connection.setConnectTimeout(300000); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect(); }catch(Exception e){
e.printStackTrace();
br = new BufferedReader(new InputStreamReader(connection.getErrorStream()) );
String line;
try {
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
connection.disconnect();
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonString.toString();
}
public static String sendPostRequestH(String requestUrl, String payload) {
StringBuffer jsonString = new StringBuffer();
HttpURLConnection connection=null;
BufferedReader br=null;
try {
URL url = new URL(requestUrl);
connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("resultData", payload);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write("");
writer.close();
br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect(); }catch(Exception e){
e.printStackTrace();
br = new BufferedReader(new InputStreamReader(connection.getErrorStream()) );
String line;
try {
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
connection.disconnect();
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonString.toString();
} /**
* PUT
**/ public static String doPut(String strUrl,String param){
StringBuffer jsonString = new StringBuffer();
HttpURLConnection httpCon=null;
BufferedReader br=null;
try{
URL url = new URL(strUrl);
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
httpCon.getOutputStream());
out.write(param);
out.close();
br = new BufferedReader(new InputStreamReader(httpCon.getInputStream())); String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
} }catch(Exception e){
e.printStackTrace();
br = new BufferedReader(new InputStreamReader(httpCon.getErrorStream()) );
String line;
try {
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
httpCon.disconnect();
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonString.toString();
} public static String getHTML2(String urlToRead) throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlToRead);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
try{
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
}catch(Exception e){ System.out.println(conn.getResponseCode()); } return result.toString();
} /**
* GET
**/
public static String getHTML(String urlToRead) throws Exception {
BufferedReader br=null;
StringBuilder result =null;
HttpURLConnection conn=null;
try{
result = new StringBuilder();
URL url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
result.append(line);
}
}catch(Exception e){
e.printStackTrace();
br = new BufferedReader(new InputStreamReader(conn.getErrorStream()) );
String line;
try {
while ((line = br.readLine()) != null) {
result.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
conn.disconnect();
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return result.toString();
}
public static String getHTML(String urlToRead,String payload) throws Exception {
StringBuffer jsonString = new StringBuffer();
BufferedReader br=null;
URL url = new URL(urlToRead);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try { connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
br = new BufferedReader(new InputStreamReader(connection.getErrorStream()) );
String line;
try {
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return jsonString.toString();
}
public static String getHTMLXQ(String urlToRead,String payload) throws Exception {
StringBuffer jsonString = new StringBuffer();
BufferedReader br=null;
URL url = new URL(urlToRead);
//http://nealcai.iteye.com/blog/2084442
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Cookie", "Hm_lvt_1db88642e346389874251b5a1eded6e3=1529975814,1530498921,1530522215,1530522465; __utma=1.2081209674.1521790939.1527061820.1530522218.3; __utmz=1.1530522218.3.3.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; device_id=eee17d8ff8702c633d2e5aef40091e3b; _ga=GA1.2.2081209674.1521790939; aliyungf_tc=AQAAANJkmXYQ0AUADTpltt1aHoUGzyIQ; xq_a_token=7443762eee8f6a162df9eef231aa080d60705b21; xq_a_token.sig=3dXmfOS3uyMy7b17jgoYQ4gPMMI; xq_r_token=9ca9ab04037f292f4d5b0683b20266c0133bd863; xq_r_token.sig=6hcU3ekqyYuzz6nNFrMGDWyt4aU; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1530522562; _gid=GA1.2.257714409.1530498922; u=941530498925106; __utmc=1; s=e81dkxiqhm");
connection.setRequestProperty("Referer", "https://xueqiu.com/S/SH600507/ZYCWZB");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0");
connection.setRequestProperty("Host", "xueqiu.com");
connection.setRequestProperty("Upgrade-Insecure-Requests", "1");
connection.setRequestProperty("Cache-Control", "max-age=0");
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
br.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
br = new BufferedReader(new InputStreamReader(connection.getErrorStream()) );
String line;
try {
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return jsonString.toString();
} /**
* DELETE
**/
public static void deleteHTML(String urlToRead) throws Exception {
URL url = new URL(urlToRead);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.disconnect(); } public static String deleteHTML(String strUrl,String param){
StringBuffer jsonString = new StringBuffer();
HttpURLConnection httpCon=null;
BufferedReader br=null;
try{
URL url = new URL(strUrl);
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("DELETE");
OutputStreamWriter out = new OutputStreamWriter(
httpCon.getOutputStream());
out.write(param);
out.close();
br = new BufferedReader(new InputStreamReader(httpCon.getInputStream())); String line;
while ((line = br.readLine()) != null) {
jsonString.append(line);
} }catch(Exception e){
e.printStackTrace();
br = new BufferedReader(new InputStreamReader(httpCon.getErrorStream()) );
String line;
try {
while ((line = br.readLine()) != null) {
jsonString.append(line);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
httpCon.disconnect();
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonString.toString();
} }
HTTP请求代理类(GET 、 POST 、PUT 、DELETE)的更多相关文章
- HTTP请求状态类
<?php /** * 常用常量文件 * */ /** * HTTP协议请求状态 */ class HttpRequest { //100类 ----用于指定客户端应相应的某些动作---- co ...
- C# http请求工具类
/// <summary> /// Http请求操作类之HttpWebRequest /// </summary> public class HttpHelper { #reg ...
- Struts2 源码分析——Action代理类的工作
章节简言 上一章笔者讲到关于如何加载配置文件里面的package元素节点信息.相信读者到这里心里面对struts2在启动的时候加载相关的信息有了一定的了解和认识.而本章将讲到关于struts2启动成功 ...
- 解析利用wsdl.exe生成webservice代理类的详解
利用wsdl.exe生成webservice代理类:根据提供的wsdl生成webservice代理类1.开始->程序->Visual Studio 2005 命令提示2.输入如下红色标记部 ...
- 【C++沉思录】代理类
1.考虑下面的场景:设计一个容器,包含一组类型不同但相互关联的对象(比如:Animal,Dog,Cat),对象具备多态行为.2.容器一般只能包含一种类型的对象,使用vector<Animal&g ...
- 根据WSDL生成代理类方式
方式一: 1.使用VS2010提供的工具wsdl.exe由WSDL文件生成cs文件 使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转 ...
- C++的代理类
怎样在一个容器中包含类型不同,但是彼此有关系的对象?众所周知,C++的容器只能存放类型相同的元素,所以直接在一个容器中存储不同类型的对象本身是不可能的,只能通过以下两种方案实现: 1. 提供一个间接层 ...
- c++沉思录 学习笔记 第五章 代理类
Vehicle 一个车辆的虚基类 class Vehicle {public: virtual double weight()const = 0; virtual void start() = 0; ...
- .net 代理类(WebService代理类的详解 )
http://hi.baidu.com/654085966/item/53ee8c0f108ad78202ce1b1d -----------转自 客户端调用Web Service的方式我现在知道 ...
随机推荐
- SharePoint创建内容类型
SharePoint创建内容类型 内容类型的用途是多种多样的.创建内容类型也非常简单. 1. 点击网站操作--网站设置. 2. 点击网站内容类型,点击创建. 3. 命名Beginning_ShareP ...
- WPF中ListBox滚动时的缓动效果
原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...
- C#读取文件夹特定文件的方法
public image[] getImages() { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog ...
- 动态lambda 构建
var param = Expression.Parameter(typeof(T)); var datetime1 = Expression.Constant(dt1); var datetime2 ...
- 从零开始学习音视频编程技术(三) 开发环境搭建(Qt4.86手动设置环境,主要就是设置g++和qmake,比较透彻,附下载链接)
1.先下载安装Qt 我们使用的版本是4.8. 可以自行百度下载也可以从下面的网盘地址下载: Qt库和编译器下载: 链接:http://pan.baidu.com/s/1hrUxLIG 密码:0181 ...
- PHP MYSQL 获取记录总数
$qid = mysql_query(“SELECT count(aid) as total FROM table group by aid “);//你的查询 $res = mysql_fetch_ ...
- 海康SDK编程指南
转至心澄欲遣 目前使用的海康SDK包括IPC_SDK(硬件设备),Plat_SDK(平台),其中两套SDK都需单独调用海康播放库PlayCtrl.dll来解码视频流,返回视频信息和角度信息.本文仅对视 ...
- hMailServer搭建简单邮件系统
本文介绍的是搭建本地的邮件系统,至于互联网的还在研究之中. 1.需要一个邮件服务器软件,这里用的是hMailServer,其中会让你设置一个密码,记住这个密码,后面连接的时候回用到. 2.添加域名 因 ...
- .NET DataTable转换为JSON格式的字符串
在进行数据传递的时候,有时我们需要通过Ajax的方式或者其他的方式传递一个数据列表,可以将DataTable或者其他形式的数据列表转换为JSON的格式,通过Ajax实体的形式进行传递. 比如说: // ...
- 论文阅读计划1(Benchmarking Streaming Computation Engines: Storm, Flink and Spark Streaming & An Enforcement of Real Time Scheduling in Spark Streaming & StyleBank: An Explicit Representation for Neural Ima)
Benchmarking Streaming Computation Engines: Storm, Flink and Spark Streaming[1] 简介:雅虎发布的一份各种流处理引擎的基准 ...