package test2;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BridgeServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String relatedtUrl = request.getParameter("RedirectURL");
StringBuffer url = new StringBuffer("http://172.18.33.33:9704/analytics/" + relatedtUrl);
Map parameterMap = request.getParameterMap();
for (Iterator iterator = parameterMap.keySet().iterator(); iterator
.hasNext();) {
String parameterName = (String) iterator.next();
String[] parameterValues = (String[]) parameterMap
.get(parameterName);
if (parameterValues != null && parameterValues.length > 0) {
if (parameterName.equals("RedirectURL")) {
continue;
}
for (int i = 0; i < parameterValues.length; i++) {
url.append("&");
url.append(parameterName);
url.append("=");
String value = "";
try {
value = URLEncoder.encode(parameterValues[i], "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
url.append(value);
}
}
}
try {
URL urlconn = new URL(url.toString());
URLConnection httpConnection = (URLConnection) urlconn
.openConnection();
((HttpURLConnection) httpConnection).setRequestMethod("GET");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setUseCaches(false);
httpConnection.setRequestProperty("Content-Type", "binary/data");
httpConnection.setRequestProperty("User-Agent", request
.getHeader("USER-AGENT"));
InputStream in = (InputStream) httpConnection.getInputStream();
ServletOutputStream out = response.getOutputStream();
byte buffer1[] = new byte[1024 * 128];
int k = 0;
while ((k = in.read(buffer1)) != -1) {
out.write(buffer1, 0, k);
}
in.close();
out.close();
((HttpURLConnection) httpConnection).disconnect();
} catch (Exception e) {
System.out.println("网络连接问题!无需大惊小怪,暂无解决办法!byebye!");
e.printStackTrace();
}
}
}