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的库,原因在于这个第三方库变化实在太快了,每个版本都有不小的变化.对于程序员来说,使用它反而会有很多麻烦,比如自己曾经写过的代码将无法复用. 原 ...
随机推荐
- Mac下 Docker部署SpringBoot应用
一.安装Docker环境 使用 Homebrew 安装 macOS 我们可以使用 Homebrew 来安装 Docker. Homebrew 的 Cask 已经支持 Docker for Mac,因此 ...
- Python Flask 蓝图Blueprint
1. 目录结构 2. manage.py类似于django中manage import fcrm if __name__ == '__main__': fcrm.app.run(port=8001) ...
- mysql-表完整性约束
阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 总结 一 介绍 回到顶 ...
- WordPress主循环(The Loop)函数have_posts(),the_post()详解
WordPress中调用文章标题是the_title();调用文章内容时用到the_content();调用文章的作者时用到the_author();等等这些函数,都需要在主循环中使用,下面就介绍一下 ...
- git: Your branch and 'origin/master' have diverged
git: Your branch and 'origin/master' have diverged - how to throw away local commits? - Stack Overfl ...
- javascript大神修炼记(2)——运算符
读者朋友们好,前面我已经大概的了解了Javascript的作用以及一些基本的函数声明与变量声明,今天我们就接着前面的内容讲解,我们就来看一下javscript的逻辑(正序,分支,循环)以及一些简单的运 ...
- vs2005 QT4.7.1编译 详细
http://blog.csdn.net/debugconsole/article/details/8230683 网上一搜有QT+2005编译的很多文章,但是都不详细,很多都编不过,特别的在conf ...
- (2)go 规范,变量,基本数据类型
.一.规范 main 函数为入口 严格区分大小写 不需要加分号 一行只写一条语句 定义的变量或者导入的包必须要用到,否则编译时会报错 左括号不能单独一行 逗号可以用来在换行时连接字符串 标识符由字母数 ...
- 链式前向星实现的堆优化dij求最短路模板
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...
- redis缓存总结----干货
Redis的概念 Redis是一款内存高速缓存数据库.Redis全称为:Remote Dictionary Server(远程数据服务),Redis是一个key-value存储系统,它支持丰富的数据类 ...