如何通过HttpURLConnection得到http 302的跳转地址

如果不想直接跳转到目标地址,想对中间的Location做些处理则可以通过HttpURLConnection实现。 // 必须设置false,否则会自动redirect到Location的地址 conn.setInstanceFollowRedirects(false);

package com.zuidaima;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 如何通过HttpURLConnection得到http 302的跳转地址
 * 
 * @author javaniu
 * 
 */
public class Http302 {

	public static void main(String[] args) {
		try {
			String url = "http://www.zuidaima.com/302.htm";
			System.out.println("访问地址:" + url);
			URL serverUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) serverUrl
					.openConnection();
			conn.setRequestMethod("GET");
			// 必须设置false,否则会自动redirect到Location的地址
			conn.setInstanceFollowRedirects(false);

			conn.addRequestProperty("Accept-Charset", "UTF-8;");
			conn.addRequestProperty("User-Agent",
					"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
			conn.addRequestProperty("Referer", "http://zuidaima.com/");
			conn.connect();
			String location = conn.getHeaderField("Location");

			serverUrl = new URL(location);
			conn = (HttpURLConnection) serverUrl.openConnection();
			conn.setRequestMethod("GET");

			conn.addRequestProperty("Accept-Charset", "UTF-8;");
			conn.addRequestProperty("User-Agent",
					"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
			conn.addRequestProperty("Referer", "http://zuidaima.com/");
			conn.connect();
			System.out.println("跳转地址:" + location);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

	    			

转载:http://www.zuidaima.com/share/1550463509482496.htm

发布了673 篇原创文章 · 获赞 2206 · 访问量 533万+
展开阅读全文

HTTP/1.1 302 Moved Temporarily, 这个http是连接超时吗

03-09

我模拟登录 提交了一个from表单,header,param 都获取到了,请求的结果怎么是 HTTP/1.1 302 Moved Temporarily 是吗请求的url地址不对么 String url = "http://m44.mm.mail.163.com/auth/login.s?"; service = new RawHttpClientService(HOST,80,false); ArrayList<NameValuePair> header = Lists.newArrayList(); ArrayList<NameValuePair> params = Lists.newArrayList(); header.add(new BasicNameValuePair("Accept-Language", ACCEPT_LANGUAGE)); header.add(new BasicNameValuePair("User-Agent", USER_AGENT)); header.add(new BasicNameValuePair("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")); header.add(new BasicNameValuePair("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7")); header.add(new BasicNameValuePair("Accept-Encoding", ACCEPT_ENCODING)); header.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded")); header.add(new BasicNameValuePair("Connection:", "keep-alive")); header.add(new BasicNameValuePair("Origin:", "http://m.mail.163.com")); header.add(new BasicNameValuePair("Host", HOST)); header.add(new BasicNameValuePair("Cookie:", cookieContainer.toString())); params.add(new BasicNameValuePair("method","login")); params.add(new BasicNameValuePair("back_url","http://m44.mm.mail.163.com/xm/welcome.do")); params.add(new BasicNameValuePair("timestamp","0")); params.add(new BasicNameValuePair("m","1")); params.add(new BasicNameValuePair("register","0")); params.add(new BasicNameValuePair("wml","false")); params.add(new BasicNameValuePair("srandid","")); params.add(new BasicNameValuePair("ucid","")); params.add(new BasicNameValuePair("publicExponent",loginParams.get("publicExponent").toString())); params.add(new BasicNameValuePair("modulus",loginParams.get("modulus").toString())); params.add(new BasicNameValuePair("username",loginParams.get("username").toString())); params.add(new BasicNameValuePair("domain",loginParams.get("domain").toString()));//163.com 126.com yeah.net vip.163.com vip.126.com 188.com params.add(new BasicNameValuePair("password",enctry(loginParams.get("publicExponent").toString(),loginParams.get("modulus").toString(),loginParams.get("password").toString()))); Pair<List<NameValuePair>, String> response = service.request(url, HttpMethod.POST, header, params, new DefaultResponseHandle(), ENCODE); cookieContainer.accept(response.getFirst()); 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览