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();
} }

最新文章

  1. Genymotion安装问题
  2. 【转】成为Java顶尖程序员 ,看这10本书就够了
  3. 谷歌浏览器下载地址 chrome最新版本 百度云地址
  4. Excel替换应用
  5. Tomcat源码分析
  6. php面向对象中的静态与抽象,接口
  7. 图解 & 深入浅出 JavaWeb:Servlet 再说几句
  8. BZOJ3720 Gty的妹子树
  9. std::shared_ptr(二)
  10. 背景大图隔几秒切换(非轮播,淡入淡出)--变形金刚joy007 项目总结
  11. 简短总结一下C#里跨线程更新UI
  12. 垃圾回收 GC
  13. linux-0.11内核 调试教程+GCC源代码
  14. bzoj 3240: [Noi2013]矩阵游戏 矩阵乘法+十进制快速幂+常数优化
  15. LR报-27727错误解决办法
  16. c语言单链表,冒泡排序
  17. VFS四大对象之一 struct super_block
  18. MATLAB 曲线形状,粗细,颜色使用大全
  19. socket基础编程-2
  20. 练手THINKPHP5过程和bootstrap3.3.7

热门文章

  1. 简单的讲Erlang一些运营商
  2. 【LeetCode】LRU Cache 解决报告
  3. Android Widget 小工具(两) 使用configure
  4. sql server 查询存储过程指令
  5. Bootstrap 单按钮下拉菜单
  6. 【转】跟面试官聊.NET垃圾收集,直刺面试官G点
  7. 【Windows10 IoT开发系列】配置篇
  8. webform的图片防盗链
  9. TStringGrid多选的复制与拷贝
  10. C#WinForm线程基类