一、文章前言
一念之间的坚持换来更好的自己
u=84224111,892651579&fm=27&gp=0.jpg
二、内容:
有时候我们需要用java代码模仿浏览器给某个服务器发送http请求(本文将以get和post请求来展示)。
1、首先我们先来看看get请求(文章所请求得服务器就以百度搜索为例):
1.我们打开百度主页按下F12或者右键查看网页源码NetWork
2.可以简单的发现请求url是"https://www.baidu.com/s?wd=(这里是你想查询的东西也就是get请求的参数)"
下面放上代码
private static StringBuilder buildConnection() throws IOException {
//String path = "https://www.baidu.com/s?wd=(...)";根据实际情况来
String path = "https://www.reg007.com/search?q=xxxx@qq.com";
URL url;
HttpURLConnection connection;
url = new URL(path);
connection = (HttpURLConnection) url.openConnection();
//设置请求的方式
connection.setRequestMethod("GET