重定向 and post请求




如果网址重定向(301,302,304)

if(responseCode/100==3){

String redirect=urlConnection.getHeaderField("Location");

urlConnection.disconnect();

urlConnection= (HttpURLConnection) new URL(redirect).openConnection();

//延时操作

requestTimeOut(urlConnection);

responseCode=urlConnection.getResponseCode();

if (responseCode == 200) {

String textString=streamString(urlConnection.getInputStream());

urlConnection.disconnect();

handler.sendMessage(handler.obtainMessage(info, textString));

}

 

}





Post请求

String path="http://v.juhe.cn/toutiao/index";

try {

//1.路径

URL url=new URL(path);

//2.打开连接

HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();

//设置类型

urlConnection.setRequestMethod("POST");

//延时操作

RequestTimeOut(urlConnection);

//3. 发送数据给服务端

//请求传值方式

urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

//标识需要输出

urlConnection.setDoOutput(true);

//输出数据

urlConnection.getOutputStream().write("type=top&key=597b4f9dcb50e051fd725a9ec54d6653".getBytes());

//4. 响应状态

int responseCode = urlConnection.getResponseCode();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值