HttpURLConnection学习笔记
文章目录
1 GET请求方式
1.1 设置要访问的url
String data = "username=tanguganlin&password=123";
URL url = new URL("http://127.0.0.1:8080/jsp_servlet_project/MyHttpServlet?"+data);
1.2 打开连接
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
1.3 设置请求方式
//3.设置请求方式 get和post必须全大写
connection.setRequestMethod("GET");
1.4 设置连接超时时间
//设置连接超时时间
connection.setConnectTimeout(10000);
//设置读取超时时间
connection.setReadTimeout(5000);
1.5 获取返回码
int responseCode = connection.getResponseCode();
1.6 获取返回数据
InputStream inputStream = connection.getInputStream();
int length = 0;
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer();
while ((length = inputStream.read(buff))!=-1){
sb.append(new String(buff, 0, length));
}
String content = sb.toString();
get提交案例
package com.tangguanlin.httpurlconnection;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 说明:HttpURLConnection get提交测试类
* 作者:汤观林
* 日期:2022年02月27日 16时
*/
public class HttpURLConnectionGetTest {
public static void main(String[] args){
HttpURLConnection connection = null;
try {
String data = "username=tanguganlin&password=123";
//1.设置要访问的url
URL url = new URL("http://127.0.0.1:8080/jsp_servlet_project/MyHttpServlet?"+data);
//2.打开连接
connection = (HttpURLConnection)url.openConnection();
//3.设置请求方式 get和post必须全大写
connection.setRequestMethod("GET");
//4.设置连接超时时间
connection.setConnectTimeout(10000);
//设置读取超时时间
connection.setReadTimeout(5000);
//5.获取返回码
int responseCode = connection.getResponseCode();
System.out.println("响应码:"+responseCode);
if(responseCode==200){
//6.获取返回数据
InputStream inputStream = connection.getInputStream();
int length = 0;
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer();
while ((length = inputStream.read(buff))!=-1){
sb.append(new String(buff, 0, length));
}
String content = sb.toString();
System.out.println("响应内容是:"+content);
}else{
//访问失败
System.out.println("访问失败:"+responseCode);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(connection!=null){
connection.disconnect(); //关闭连接
}
}
}
}
运行结果:
响应码:200
响应内容是:用户名:tanguganlin 密码:123
用户验证成功,谢谢访问
2 POST请求方式
2.1 设置要访问的url
URL url = new URL("http://127.0.0.1:8080/jsp_servlet_project/MyHttpServlet");
2.2 打开连接
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
2.3 设置请求方式
//设置请求方式 get和post必须全大写
connection.setRequestMethod("POST");
2.4 设置连接超时时间
//设置连接超时时间
connection.setConnectTimeout(10000);
//设置读取超时时间
connection.setReadTimeout(5000);
加1 设置请求头信息
//加1:设置请求头信息 可以设置多个,服务器端不需要,也可以不传
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("Pragma","no-cache");
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
加2 设置请求参数
//post请求的参数
String data = "username=tanguganlin&password=123";
//获取输出流,用于向服务器写数据,默认情况下,系统不允许向服务器输出内容
connection.setDoOutput(true); //允许输出
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes());
outputStream.close();
2.5 获取返回码
int responseCode = connection.getResponseCode();
2.6 获取返回数据
InputStream inputStream = connection.getInputStream();
int length = 0;
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer();
while ((length = inputStream.read(buff))!=-1){
sb.append(new String(buff, 0, length));
}
String content = sb.toString();
post提交案例
package com.tangguanlin.httpurlconnection;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 说明:HttpURLConnection post提交 测试类
* 作者:汤观林
* 日期:2022年02月27日 18时
*/
public class HttpURLConnectionPostTest {
public static void main(String[] args) {
HttpURLConnection connection = null;
try {
//1.设置要访问的url
URL url = new URL("http://127.0.0.1:8080/jsp_servlet_project/MyHttpServlet");
//2.打开连接
connection = (HttpURLConnection)url.openConnection();
//3.设置请求方式 get和post必须全大写
connection.setRequestMethod("POST");
//4.设置连接超时时间
connection.setConnectTimeout(10000);
//设置读取超时时间
connection.setReadTimeout(5000);
//加1:设置请求头信息 可以设置多个,服务器端不需要,也可以不传
connection.setRequestProperty("Cache-Control","no-cache");
connection.setRequestProperty("Pragma","no-cache");
connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
//post请求的参数
String data = "username=tanguganlin&password=123";
//加2:设置请求参数
//获取输出流,用于向服务器写数据,默认情况下,系统不允许向服务器输出内容
connection.setDoOutput(true); //允许输出
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes());
outputStream.close();
//5.获取返回码
int responseCode = connection.getResponseCode();
System.out.println("响应码:"+responseCode);
if(responseCode==200){
//6.获取返回数据
InputStream inputStream = connection.getInputStream();
int length = 0;
byte[] buff = new byte[1024];
StringBuffer sb = new StringBuffer();
while ((length = inputStream.read(buff))!=-1){
sb.append(new String(buff, 0, length));
}
String content = sb.toString();
System.out.println("响应内容是:"+content);
}else{
//访问失败
System.out.println("访问失败:"+responseCode);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(connection!=null){
connection.disconnect(); //关闭连接
}
}
}
}
运行结果:
响应码:200
响应内容是:用户名:tanguganlin 密码:123
用户验证成功,谢谢访问
3 被请求的第三方
3.1 浏览器请求地址
http://localhost:8080/jsp_servlet_project/MyHttpServlet?username=tanguanlin&password=123
3.2 第三方代码
package com.tangguanlin;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 说明:被请求的第三方
* 作者:汤观林
* 日期:2022年02月02日 21时
*/
public class MyHttpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("用户名:"+username+" 密码:"+password);
out.println("用户验证成功,谢谢访问");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("走的doPost方法");
doGet(req,resp);
}
}
3.3 运行结果
用户名:tanguanlin
密码:123
用户验证成功,谢谢访问