Java发送http请求(httpUrlConnection)安卓客户端也可以使用
2017-10-21 11:02
871 查看
一、文章前言
一念之间的坚持换来更好的自己u=84224111,892651579&fm=27&gp=0.jpg
二、内容:
有时候我们需要用java代码模仿浏览器给某个服务器发送http请求(本文将以get和post请求来展示)。1、首先我们先来看看get请求(文章所请求得服务器就以百度搜索为例):
1.我们打开百度主页按下F12或者右键查看网页源码NetWork2.可以简单的发现请求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"); connection.connect(); //获取请求返回的输入流 InputStream inputStream = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader); StringBuilder result = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { result.append(line); } connection.disconnect(); bufferedReader.close(); inputStream.close(); reader.close(); return result; }
方法运行结果:
E1@[@BKL]_EJH7_)FU7F8E8.png
获取到了网页的源码(可以给利用jsoup当成爬虫使用)
**2、下面是post请求
代码如下:private static StringBuilder buildConnection() throws IOException { //String path = "https://www.baidu.com/s?wd=(...)";根据实际情况来 String path = ""; URL url; HttpURLConnection connection; url = new URL(path); connection = (HttpURLConnection) url.openConnection(); //设置请求的方式 4000 connection.setRequestMethod("POST"); //post请求必须设置下面两行 connection.setDoInput(true); connection.setDoOutput(true); /** * JSON数据的请求 * outputStream.write(stringJson.getBytes(), 0, stringJson.getBytes().length); * outputStream.close(); * **/ OutputStream outputStream = connection.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); String username= ""; String password = ""; String content = "username=" + username + "&password=" + password; dataOutputStream.writeBytes(content); dataOutputStream.flush(); dataOutputStream.close(); connection.connect(); //获取请求返回的输入流 InputStream inputStream = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader); StringBuilder result = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { result.append(line); } connection.disconnect(); bufferedReader.close(); inputStream.close(); reader.close(); return result; }
以上代码就不给测试了因为post请求测起来就需要一些个人的信息了,为了防止信息泄露。
如果读者有什么问题请在评论区留言。
(如果有不正确的地方请指正)
相关文章推荐
- 在Java中使用HttpURLConnection发送http客户端请求、服务器端返回信息、接受服务器端的信息
- 在JAVA中使用HttpURLConnection发送POST请求
- 使用httpurlconnection发送http请求
- 使用java.net.URLConnection发送http请求
- 使用HttpURLConnection发送HTTP请求
- Java使用HttpURLConnection发送Get及Post请求简单过程
- Android上使用HttpURLConnection发送HTTP请求
- 【JAVA】通过URLConnection/HttpURLConnection发送HTTP请求的方法(一)
- 第一行代码总结:10网络:10.2使用HTTP协议访问网络10.2.1 使用HttpURLConnection10.2使用HTTP协议访问网络 工作原理: 客户端向服务器发出一条HTTP请求,服务器收
- JAVA中使用HttpURLConnection发送XML参数乱码问题
- Android使用HttpURLConnection发送HTTP请求
- 使用java调用http请求系列--HttpURLConnection
- java 使用HttpURLConnection发送数据简单实例
- JAVA使用原始HttpURLConnection发送POST数据
- Android中使用HttpURLConnection和HttpClient发送Http请求
- Android 使用HttpURLConnection发送HTTP请求
- Java 发送http请求 HttpURLConnection
- HttpURLConnection从客户端向服务器发送Http请求以及服务器响应全过程解析
- 使用httpclient实现http链接池与使用HttpURLConnection发送http请求的方法与性能对比
- Java 利用HttpURLConnection发送http请求