淘客链接转成正常淘宝链接JAVA代码实现

时间:2025-03-29 07:27:25

更新1:2018/1/15


类似于 /t?e=m%3D2%26s%3DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%2B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%3D&pvid=10_183.135.6.227_21727_1512373605774&sc=c2Z10Xw&ref=&et=nQZarJKAELgKLFmnQOOdGhGI9MyeD0rm


这种链接怎么转成正常的淘宝链接??


实际问题:淘客链接转成正常商品链接


解决办法:
1、获取:tu链接=/t_js?tu=https%3A%2F%%2Ft%3Fe%3Dm%253D2%2526s%253DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%252B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%253D%26pvid%3D10_183.135.6.227_21727_1512373605774%26sc%3Dc2Z10Xw%26ref%3D%26et%3DXDdI5IoTDHiv%252FPC6VebP6npa%252BXvuZjy1


2、解密tu参数链接:String jiemiUrl = "/t?e=m%3D2%26s%3DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%2B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%3D&pvid=10_183.135.6.227_21727_1512373605774&sc=c2Z10Xw&ref=&et=XDdI5IoTDHiv%2FPC6VebP6npa%2BXvuZjy1";
3、 设置请求行referer: (, tu);
重新获取location: (getBaseUrl(jiemiUrl));

请看代码实现:


public class GetRedirectUrl {
	@Test
	public void test_getRedirectUrl() throws Exception {
		// String
		// url="/link?url=ByBJLpHsj5nXx6DESXbmMjIrU5W4Eh0yg5wCQpe3kCQMlJK_RJBmdEYGm0DDTCoTDGaz7rH80gxjvtvoqJuYxK";
		String url = "/goto_261499.aspx";
		String expectUrl = "/question/20583607/answer/16597802";
		/**
		 * 0、淘宝客链接跳转淘宝链接
		 */
		// 递归;
		String tu = getRedirectUrl(url);
		;
		("tu链接" + tu);
		// 解密tu参数链接
		// ((("tu=")+3));
		String jiemiUrl2 = unescape((("tu=") + 3));
		("tu参数解密链接" + jiemiUrl2);
		String jiemiUrl = "/t?e=m%3D2%26s%3DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%2B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%3D&pvid=10_183.135.6.227_21727_1512373605774&sc=c2Z10Xw&ref=&et=nQZarJKAELgKLFmnQOOdGhGI9MyeD0rm";
		// String jiemiUrl =
		// "/t?e=m%3D2%26s%3DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%2B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%3D&pvid=10_183.135.6.227_21727_1512373605774&sc=c2Z10Xw&ref=&et=XDdI5IoTDHiv%2FPC6VebP6npa%2BXvuZjy1";
		// // 请求获取(tu参数链接);
		("淘宝链接:" + getBaseUrl(jiemiUrl2, tu));

		/**
		 * 1、普通的跳转获取(递归函数)
		 */

		// ("递归链接:"+getRedirectUrl(url));
		/**
		 * 2、不递归获取
		 */
		// ("非递归链接:"+getUrl(url));

		/**
		 * 加密、解密
		 */
		// String path1 =
		// "https%3a%2f%%2ft%3fe%3dm%253D2%2526s%253DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%252B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%253D%26pvid%3d10_183.135.6.227_21727_1512373605774%26sc%3dc2Z10Xw%26ref%3d%26et%3dXDdI5IoTDHiv%252FPC6VebP6npa%252BXvuZjy1";
		//
		// String path2 =
		// "/t?e=m%3D2%26s%3DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%2B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%3D&pvid=10_183.135.6.227_21727_1512373605774&sc=c2Z10Xw&ref=&et=XDdI5IoTDHiv%2FPC6VebP6npa%2BXvuZjy1";
		// ("解密后:" + unescape(path1));
		// ("正确值:" + path2);
	}

	/**
	 * 递归获取重定向地址
	 * 
	 * @param path
	 * @return
	 * @throws Exception
	 */

	private String getRedirectUrl(String needRedirectUrl) throws Exception {

		HttpURLConnection conn = (HttpURLConnection) new URL(needRedirectUrl).openConnection();
		(false);
		// (8000);
		/**
		 * 递归找到最终的url(包含location)
		 */
		if (("location") == null)
			return needRedirectUrl;
		else
			return getRedirectUrl(("location"));
		// }

		/**
		 * 方法二
		 */
		// //创建一个http请求
		// HttpClient client = new HttpClient();
		// //用header的请求方式,减少返回值和一些非必要获取的信息
		// HttpMethod method = new HeadMethod(url);
		// HttpParams params = ();
		// (AllClientPNames.HANDLE_REDIRECTS, false);
		// (method);
		// //获取链接
		// url = ().getURI();
		// //释放链接(重要)
		// ();
	}

	/**
	 * 不递归获取跳转链接
	 * 
	 * @author Yien
	 * @when 2017年12月5日 下午4:09:47
	 *
	 *
	 * @param path
	 * @return
	 * @throws Exception
	 */

	private String getUrl(String path) throws Exception {

		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) ();
		();
		(true);
		String realUrl = ().toString();
		();
		// (realUrl);
		return realUrl;
	}

	/**
	 * 淘宝客链接最后跳转淘宝链接
	 * 
	 * @author Yien
	 * @when 2017年12月5日 下午4:03:40
	 *
	 *
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public String getBaseUrl(String redirectUrl, String tu) throws Exception {
		HttpURLConnection conn = (HttpURLConnection) new URL(redirectUrl).openConnection();
		(false);
		// (8000);
		/**
		 * tu为包含location的最终url
		 */
		// String tu =
		// "/t_js?tu=https%3A%2F%%2Ft%3Fe%3Dm%253D2%2526s%253DshhycMgyX5YcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67gtlG6lwPBS0UkCu4LW4fHWggE%252B81xYdaYgwvAJKzKRhR0QUH89hs6odoD3G8jExAxAqvUhwaPOiypAbhWWbtazmAClNJkpsCzHrKHhQWCLYttvJ9Z4G1Zg%253D%26pvid%3D10_183.135.6.227_21727_1512373605774%26sc%3Dc2Z10Xw%26ref%3D%26et%3DXDdI5IoTDHiv%252FPC6VebP6npa%252BXvuZjy1";
		/**
		 * 设置请求行
		 */
		(, tu);
		// (());
		return getRedirectUrl(("Location"));

	}

	/**
	 * URL解密
	 * 
	 * @author Yien
	 * @when 2017年12月5日 下午4:26:51
	 *
	 *
	 * @param str
	 * @return
	 */

	public String unescape(String str) {

		try {
			// 将application/x-www-from-urlencoded字符串转换成普通字符串
			return (str, "GBK");

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			();
			return null;
		}

	}

	/**
	 * Url加密
	 * 
	 * @author Yien
	 * @when 2017年12月5日 下午4:27:04
	 *
	 *
	 * @param str
	 * @return
	 */
	public String escape(String str) {

		try {
			// 将普通字符创转换成application/x-www-from-urlencoded字符串
			return (str, "GBK");

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			();
			return null;
		}

	}
}

更新1简洁版代码:
import ;

import ;
import ;
import ;
import ;

public class 转淘宝链接 {

public static void main(String[] args) {
		String urlStr = "/t?e=zGU34CA7K%2BPkqB07S4%2FK0CITy7klxxrJ35Nnc0ls" +
		"8UNch6eorWAPrhuAvw2mQW2OJDeSwknfCSiKd622D%2BDnIc9QSYrG4navJ4t7sfX4ek43cWA%3D";
        HttpClient client;
        client = new HttpClient(new MultiThreadedHttpConnectionManager());
        ().getParams().setConnectionTimeout(10000);
        ().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");  //让服务器认为是IE

        GetMethod get = new GetMethod(urlStr);
        
        (false); //禁止自动重定向


        try {
            int iGetResultCode = (get); 
            String _refer = ("location").getValue(); //打印地址
            GetMethod get2 = new GetMethod((_refer.split("tu=")[1]));
            ("Referer", _refer);
            (false); //禁止自动重定向
            int iGetResultCode2 = (get2); 
            String realUrl = ("location").getValue(); //打印地址
            ("REAL URL: "+realUrl);
            GetMethod get3 = new GetMethod(realUrl);
            int iGetResultCode3 = (get3); 
            String responseBody = ();
//            ("Response Body: "+responseBody);
            ((("sellerRate=" +
            		"")+12,("&", ("sellerRate="))));

        } catch (Exception ex) {
            ();
        } finally {
            ();
        }
}


}