java发送http的get和post请求
import java.io.*;
import java.net.URL;
import java.util.Map;
import java.net.HttpURLConnection;; public class HttpRequest{ public static String sendGet(String url) {
String result = "";
BufferedReader in = null;
try {
URL realurl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) realurl.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch(Exception e) {
System.out.println("发送get请求失败:"+e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
} public static String sendPost(String url, String params) {
String encoding = "UTF-8";
String result = "";
BufferedReader in = null;
try {
byte[] data = params.getBytes(encoding);
URL base_url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) base_url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
if (conn.getResponseCode() == 200) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
}
} catch(Exception e) {
System.out.println("发送POST请求失败:"+e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
java发送http的get和post请求的更多相关文章
- java发送http的get、post请求
转载博客:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html Http请求类 package wzh.Http; impor ...
- java发送http的get、post请求[转]
原文链接:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html package wzh.Http; import java.i ...
- java发送http的get、post请求【备忘】
类 package com.dsideal.kq.Controller; import java.io.BufferedReader; import java.io.IOException; impo ...
- [转java发送http的get、post请求]
Http请求类 package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io. ...
- 用JAVA发送一个XML格式的HTTP请求
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStr ...
- [zhuan]java发送http的get、post请求
http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html Http请求类 package wzh.Http; import jav ...
- java发送application/json格式的post请求,需要登陆
package util; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWri ...
- Java发送Http请求并获取状态码
通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...
- 编写爬虫(spider)的预备知识:用java发送HTTP请求
使用原生API来发送http请求,而不是使用apache的库,原因在于这个第三方库变化实在太快了,每个版本都有不小的变化.对于程序员来说,使用它反而会有很多麻烦,比如自己曾经写过的代码将无法复用. 原 ...
随机推荐
- Find Minimum in Rotated Sorted Array I&&II——二分查找的变形
Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...
- 何時需要重启 OFBiz
你在做如下更改時需要重新启動OFBiz服務器: - Java文件(記得要重新編譯) - 配置/.properties文件 - entitymodel或entitygroup XML定義文件 - 服務或 ...
- 微信小程序~触摸相关事件(拖拽操作、手势识别、多点触控)
touchstart 手指触摸动作开始 touchmove 手指触摸后移动 touchcancel 手指触摸动作被打断,如来电提醒,弹窗 touchend 手指触摸动作结束 ...
- OpenStack 存储服务 Cinder存储节点部署LVM (十四)
部署在block(10.0.0.103)主机 一)配置lvm 1.安装lvm2软件包 yum install lvm2 -y 2.启动LVM的metadata服务并且设置该服务随系统启动 system ...
- es6解构赋值总结
数组的解构赋值 1.简单的赋值方式 2.多维数组解构赋值 3.默认值,只有当右边对应位置为undefined时候才会选择默认(null不属于undefined) 4.左右不对等,会相应的对号入座,没有 ...
- shopnc 店铺二级分类添加商品
$class_2 = $goods_class[$gc_id]['gc_parent_id']; $class_1 = $goods_class[$class_2]['gc_parent_id']; ...
- iwebshop 增加页面访问次数实时
class里面的主控制器初始化时添加如下代码 //更新页面访问次数 $siteConfig = new Config('site_config'); $sFilePath =$siteConfig-& ...
- gvim代码补全
gvim 代码自动提示 插件 插件名:AutoComplPop 下载地址:http://www.vim.org/scripts/script.php?script_id=1879 gvim 代码模板补 ...
- 《深入理解Android2》读书笔记(二)
接之前那篇<深入理解Android2>读书笔记(一) 下面几篇来分别详细分析 Binder类作为服务端的Bn的代表,BinderProxy类作为客户端的Bp的代表,BinderIntern ...
- Queries for Number of Palindromes (区间DP)
Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabytes ...