Java实现向Web站点发送GET/POST请求
程序员文章站
2024-01-19 17:20:58
...
如何用Java实现本地调用远程接口并获取数据?本文章将告诉你答案。
废话不多说,直接撸代码。
package com.future.net;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
public class GetPostTest {
/** @Description: GET请求方法
* @Author:future
* @Date:2020/5/31/17:27
* @Param
* @return java.lang.String
*/
public static String sendGet(String url,String param){
String result = "";
String urlName = url+"?"+param;
try {
URL realUrl = new URL(urlName);
// 打开和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)" );
// 获取实际的连接
conn.connect();
Map<String ,List<String>> map = conn.getHeaderFields();
for (Map.Entry<String ,List<String >> m:map.entrySet()){
System.out.println("key:"+m.getKey()+",value:"+m.getValue());
}
// 定义BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
String line;
while ((line=in.readLine())!= null){
result += "\n"+line;
}
}catch (Exception e){
}
return result;
}
/** @Description:POST请求方法
* @Author:future
* @Date:2020/5/31/19:56
* @Param
* @return java.lang.String
*/
public static String sendPost(String url,String param){
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);
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
Map<String ,List<String>> map = conn.getHeaderFields();
for (Map.Entry<String ,List<String >> m:map.entrySet()){
System.out.println("key:"+m.getKey()+",value:"+m.getValue());
}
// 定义BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
String line;
while ((line=in.readLine())!= null){
result += "\n"+line;
}
}catch (Exception e){
System.out.println("发送POST请求出现异常:"+e.getMessage());
e.printStackTrace();
}
return result;
}
}
测试:
public class GetPostTest {
public static void main(String args[]){
// 大家注意,这里测试的地址都是我本地项目的请求地址
//(记得换成自己的地址或者网上可以访问的地址)
String url2 = "http://localhost:8080/xxx/getProductList?page=1&limit=10";
String s = GetPostTest.sendGet(url,null);
System.out.println(s);
System.out.println("==========");
String post_url = "http://localhost:8080/xxx/updateUser";
String param = "userId=1&userName=admin&description=test&pwd=666666";
String result = GetPostTest.sendPost(post_url,param );
System.out.println(new String(result.getBytes(), Charset.forName("UTF-8")));;
}
}
测试结果:
完毕!!
上一篇: 29_Flutter之Stack组件
下一篇: 155. Min Stack
推荐阅读
-
Java实现向Web站点发送GET/POST请求
-
JAVA中HTTP通信 以GET和POST方式向WEB服务器提交信息,并接收WEB服务器返回的响应
-
java中利用URLConnection发送POST或GET请求
-
Python 使用requests模块发送GET和POST请求的实现代码
-
java网络编程中向指定URL发送GET POST请求示例
-
Python 使用requests模块发送GET和POST请求的实现代码
-
JAVA发送http get/post请求,调用http接口、方法详解
-
PHP get和post向服务器发送请求
-
java中利用URLConnection发送POST或GET请求
-
Java用HttpClient3发送Get和Post请求(增强)