码迷,mamicode.com
首页 > 其他好文 > 详细

建立服务器访问其他服务器

时间:2020-12-24 11:32:18      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:mp3   except   referer   cdb   import   package   request   windows   character   

package xcy;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

@WebServlet("/list")
public class Xcy extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// 1     ?  ??
	req.setCharacterEncoding("UTF-8");
	resp.setCharacterEncoding("UTF-8");
	resp.setContentType("text/html;charset=UTF-8");
	// 2            ?
	String key = req.getParameter("key"); // ctrl + shift + L   ?   
	key = URLEncoder.encode(key,"UTF-8"); // %A8%A5%AA
	// 3        ?
	// 4         ? 
	// 5        ?
	// ? ?    ?      ??? 
	// java   ?      ??? : httpClient
		// 1>    HttpClient    
		HttpClient httpClient = new HttpClient();
		// 2>      ?      ?      GetMethod  PostMethod
		String url = "http://kuwo.cn/api/www/search/searchMusicBykeyWord?key="+key+"&pn=1&rn=30&httpsStatus=1&reqId=5087bc20-3f92-11eb-8461-5f085ce62d18";
		GetMethod getMethod = new GetMethod(url);
		// 3>?  getMehtod              ?  ?
		getMethod.setRequestHeader("Cookie", "Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1608118293; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1608118293; _ga=GA1.2.1648469662.1608118293; _gid=GA1.2.408464032.1608118293; _gat=1; kw_token=8JLSPB6J1ET");
		getMethod.setRequestHeader("csrf", "8JLSPB6J1ET");
		getMethod.setRequestHeader("Referer", "http://kuwo.cn/search/list?key=%E5%91%A8%E6%9D%B0%E4%BC%A6");
		getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36");
		// 4>?  httpClient  ?  getMethod    
		int sta = httpClient.executeMethod(getMethod);

// if(sta !=HttpStatus.SC_OK) {
// System.out.println("您的链接失效,请重新测试");
// }
// 5>? getMethod ? ? ?
byte[] body = getMethod.getResponseBody();
String data = new String(body);
// ? : ? ?
// ? : ? ? ? ? ? ?
// 6, ? ?
resp.getWriter().write(data);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

11
11
11
11
11
11
11
11
11

package xcy;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

@WebServlet("/musiconeservlet")
public class XcyOne extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1,设置编码方式
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
//2,处理请求信息
String rid = req.getParameter("rid");

	//3,使用httpclient链接酷我音乐
	HttpClient httpClient = new HttpClient();
	GetMethod getMethod = new GetMethod("https://kuwo.cn/url?format=mp3&rid="+rid+"&response=url&type=convert_url3&br=128kmp3&from=web&t=1608168223781&httpsStatus=1&reqId=80ae4c61-4006-11eb-b4b1-b5e210c11b6b");
	int sta = httpClient.executeMethod(getMethod);
	if(sta !=HttpStatus.SC_OK) {
		System.out.println("服务器出错,请重新检验");
	}
	byte[] body = getMethod.getResponseBody();
	String data = new String(body);
	//4,给出相应信息
	resp.getWriter().write(data);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	doGet(request, response);
}

}

建立服务器访问其他服务器

标签:mp3   except   referer   cdb   import   package   request   windows   character   

原文地址:https://www.cnblogs.com/hedejia/p/14157684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!