java发送http的get、post请求实现代码
作者:jingxian 时间:2023-02-05 01:46:56
Http请求类
package wzh.Http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpRequest {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
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块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
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块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}
调用方法:
public static void main(String[] args) {
//发送 GET 请求
String s=HttpRequest.sendGet("http://localhost:6144/Home/RequestString", "key=123&v=456");
System.out.println(s);
//发送 POST 请求
String sr=HttpRequest.sendPost("http://localhost:6144/Home/RequestPostString", "key=123&v=456");
System.out.println(sr);
}
标签:java,发送,http,get,post
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#使用Socket实现局域网聊天
2021-07-20 02:13:20
![](https://img.aspxhome.com/file/2023/7/119467_0s.jpg)
Java通俗易懂系列设计模式之建造者模式
2022-11-01 23:27:24
Android SurfaceView画板操作
2022-07-10 15:12:23
Android 实现闪屏页和右上角的倒计时跳转实例代码
2022-12-10 17:32:32
![](https://img.aspxhome.com/file/2023/6/123356_0s.png)
Java日常练习题,每天进步一点点(46)
2023-10-05 11:20:29
![](https://img.aspxhome.com/file/2023/0/116190_0s.png)
Java线程池Executor用法详解
2022-02-13 01:21:16
![](https://img.aspxhome.com/file/2023/2/62812_0s.jpg)
SpringBoot+docker环境变量配置详解
2022-06-13 07:37:20
![](https://img.aspxhome.com/file/2023/6/84756_0s.png)
c#数据的序列化和反序列化(推荐版)
2022-03-27 22:55:56
java图片添加水印实例代码分享
2022-06-10 04:06:03
Android内存优化操作方法梳理总结
2023-12-24 17:57:03
![](https://img.aspxhome.com/file/2023/9/137639_0s.jpg)
Java关键字instanceof用法及实现策略
2023-09-23 06:05:38
![](https://img.aspxhome.com/file/2023/0/58560_0s.png)
C#实战之备忘录的制作详解
2023-08-13 02:17:21
![](https://img.aspxhome.com/file/2023/8/116638_0s.jpg)
Winform圆形环绕的Loading动画实现代码
2023-09-09 00:13:47
![](https://img.aspxhome.com/file/2023/9/88389_0s.gif)
Android使用Intent传递组件大数据
2023-09-30 12:21:46
Android编程自定义线程池与用法示例
2021-11-27 14:46:53
BeanUtils.copyProperties在拷贝属性时忽略空值的操作
2022-07-11 05:22:04
Android蓝牙服务启动流程分析探索
2023-12-20 17:45:35
![](https://img.aspxhome.com/file/2023/1/123281_0s.png)
SpringBoot Admin用法实例讲解
2021-07-26 17:57:58
![](https://img.aspxhome.com/file/2023/9/101809_0s.png)
浅谈JVM垃圾回收有哪些常用算法
2022-02-28 16:51:56
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
2022-07-31 09:37:10
![](https://img.aspxhome.com/file/2023/9/137809_0s.jpg)