httpURLConnection-网络请求的两种方式-get请求和post请求
GET请求
/**
* 从网络获取json数据,(String byte[})
* @param path
* @return
*/
public static String getJsonByInternet(String path){
try {
URL url = new URL(path.trim());
//打开连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); if(200 == urlConnection.getResponseCode()){
//得到输入流
InputStream is =urlConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while(-1 != (len = is.read(buffer))){
baos.write(buffer,0,len);
baos.flush();
}
return baos.toString("utf-8");
}
} catch (IOException e) {
e.printStackTrace();
} return null;
}
POST请求
//获取其他页面的数据
/**
* POST请求获取数据
*/
public static String postDownloadJson(String path,String post){
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
printWriter.write(post);//post的参数 xx=xx&yy=yy
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while((len=bis.read(arr))!= -1){
bos.write(arr,0,len);
bos.flush();
}
bos.close();
return bos.toString("utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
httpURLConnection-网络请求的两种方式-get请求和post请求的更多相关文章
- 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式
一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...
- C#中Post请求的两种方式发送参数链和Body的
POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...
- 解决 SharePoint 2010 拒绝访问爬网内容源错误的小技巧(禁用环回请求的两种方式)
这里有一条解决在SharePoint 2010搜索爬网时遇到的“拒绝访问错误”的小技巧. 首先要检查默认内容访问帐户是否具有相应的访问权限,或者添加一条相应的爬网规则.如果目标资源库是一个ShareP ...
- JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理
java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...
- 探讨Netty获取并检查Websocket握手请求的两种方式
在使用Netty开发Websocket服务时,通常需要解析来自客户端请求的URL.Headers等等相关内容,并做相关检查或处理.本文将讨论两种实现方法. 方法一:基于HandshakeComplet ...
- java发送http get请求的两种方式
长话短说,废话不说 一.第一种方式,通过HttpClient方式,代码如下: public static String httpGet(String url, String charset) thro ...
- Java代码模拟http请求的两种方式
z这里用百度地图的逆地理编码接口为例, 第一种方式:(通过jdk中的java.net包) 引入工具类 import java.net.URL; import java.net.URLConnectio ...
- android 向serverGet和Post请求的两种方式,android向server发送文件,自己组装协议和借助第三方开源
一个适用于Android平台的第三方和apache的非常多东西类似,仅仅是用于Android上 我在项目里用的是这个 https://github.com/loopj/android-async-ht ...
- HttpURLConnection下载图片的两种方式
public class MainActivity extends AppCompatActivity { private ImageView iv; private String imageurl ...
随机推荐
- andorid 进度条
SeekBar类似于ProgressBar,但是ProgressBar的主要功能是让用户知道目前的状态,而SeekBar的功能在于让用户调整进度,举个例子,在音乐播放器中,可以通过调整SeekBar来 ...
- 配置Git自动补全功能
Git装好后,默认是不会有按tab补全命令的功能的.可如下配置: 去git的源码中,找到contrib/completion/git-completion.bash 将git-completion.b ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_list_items(self, locator)
def get_list_items(self, locator): """Returns the values in the select list identifie ...
- VBScript: 正则表达式(RegExp对象)
RegExp对象是VBScript中用于提供简单地正则表达式支持的对象.VBScript中所有和正则表达式有关的属性和方法都有这个对象有关联. 一.RegExp对象的属性和方法(三个属性,三个方法) ...
- POJ 1738 石子合并2 GarsiaWachs算法
石子合并(GarsiaWachs算法) 只能用该算法过!!! 详解看代码 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~ ...
- uva 11107 Life Forms
题意:给你N个串,求一个串在大于等于N/2的模板串中连续出现.如果有多解按字典序最小输出. 白书模板题.二分答案+合并模板串成一个新串,扫秒新串的height数组. 考查后缀数组+LCP #inclu ...
- git ssh key for github
第一:检查.ssh是否存在(ls -al ~/.ssh) $ ls -al ~/.ssh Lists the files in your .ssh directory, if they exist 第 ...
- 集群搭建必备:虚拟机之一实现Host-only方式上网
Host-only模式实现联网得考虑如下配置过程: 1. 安装VMware-Workstation,安装虚拟机Linux(centos.ubuntu等)完毕: 2.设置虚拟机上网方式是Host-onl ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
- Android之指南针(电子罗盘)学习
点我下载源码 5月12日更新到V5版:http://download.csdn.net/detail/weidi1989/5364243 今天,在小米的开源项目中下载了一个指南针源码学习了一下,感觉不 ...