如果网址重定向(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();