java发送get请求和post请求示例
发布时间:2022-01-30 10:45:52
java向服务端发送GET和POST请求
package com.hongyuan.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpClient {
//发送一个GET请求
public static String get(String path) throws Exception{
HttpURLConnection httpConn=null;
BufferedReader in=null;
try {
URL url=new URL(path);
httpConn=(HttpURLConnection)url.openConnection();
//读取响应
if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
StringBuffer content=new StringBuffer();
String tempStr="";
in=new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while((tempStr=in.readLine())!=null){
content.append(tempStr);
}
return content.toString();
}else{
throw new Exception("请求出现了问题!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
in.close();
httpConn.disconnect();
}
return null;
}
//发送一个GET请求,参数形式key1=value1&key2=value2...
public static String post(String path,String params) throws Exception{
HttpURLConnection httpConn=null;
BufferedReader in=null;
PrintWriter out=null;
try {
URL url=new URL(path);
httpConn=(HttpURLConnection)url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
//发送post请求参数
out=new PrintWriter(httpConn.getOutputStream());
out.println(params);
out.flush();
//读取响应
if(httpConn.getResponseCode()==HttpURLConnection.HTTP_OK){
StringBuffer content=new StringBuffer();
String tempStr="";
in=new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
while((tempStr=in.readLine())!=null){
content.append(tempStr);
}
return content.toString();
}else{
throw new Exception("请求出现了问题!");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
in.close();
out.close();
httpConn.disconnect();
}
return null;
}
public static void main(String[] args) throws Exception {
//String resMessage=HttpClient.get("http://localhost:3000/hello?hello=hello get");
String resMessage=HttpClient.post("http://localhost:3000/hello", "hello=hello post");
System.out.println(resMessage);
}
}


猜你喜欢
- C#在程序中定义和使用自定义事件可以分为以下几个步骤:步骤1:在类中定义事件using System;public class TestCl
- 传输层安全性协议(英语:Transport Layer Security,缩写作 TLS),及其前身安全套接层(Secure Sockets
- 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using System
- 以前,使用github(git)结合 IntelliJ IDEA ,可以把自己本地的测试代码,使用github网站,添加到版本管理。这样就可
- 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远
- 目录synchronized 实现原理适应性自旋(Adaptive Spinning)锁升级Java 对象头偏向锁(Biased Locki
- C#生成指定范围内的不重复随机数// Number随机数个数// minNum随机数下限// maxNum随机数上限public int[]
- 写一个简单的mybatis plus插件自动生成代码的例子pom.xml 添加配置<!-- mybatis plus 插件-->
- 函数名称 说明ActiveKeyboardLayout 激活一个不同的键盘布局,该布局必须先由 LoadKeyBoardLayout函数装载
- 这几天在做公司年会的一个抽奖软件,开始做的的时候,认为算法是很简单的,把员工的数据放进list里,把list的标号作为需要获取的随机数,根据
- 在你布局或者组件混用的时候你可能会发现 gridview 的九宫格没有完全在页面上显示,只是显示了一个局部(第一行)只有一个滚动条,还不能上
- 目录一、使用BeanFactoryPostProcessor注入Bean:第一步:创建实现SpringUtils 接口工具(组件)来获取sp
- 1.如果执行了try块没有异常,则继续运行finally块中的语句,即使try块通过return,break,或者continue于最后的语
- 目录结构:Data.xls数据: 后台页面:public void doGet(HttpServletRequest reques
- Redis模糊匹配批量删除操作,使用RedisTemplate操作: public void deleteByPrex(String pre
- IO操作字节流java.io.InputStream 输入流,主要是用来读取文件内容的。java.io.OutputStream 输出流,主
- 前言当我们写了一个方法,那么这个方法是如何被执行的呢?public int add(){ int a = 10;
- 我们深知在操作Java流对象后要将流关闭,但往往事情不尽人意,大致有以下几种不能一定将流关闭的写法:1.在try中关流,而没在finally
- 本文实例为大家分享了Android实现选项菜单的具体代码,供大家参考,具体内容如下创建选项菜单步骤:(1)覆盖Activity的onCrea
- EL全称 Expression Language(表达式语言),是jsp2.0最重要的特性之一,可以利用EL表达式来访问应用程序中的数据,来