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;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class BridgeServlet2 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 {
try {
HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setHost("172.18.33.33", 9704,
"http");
HttpMethod method = getMethod(request, response);
httpClient.executeMethod(method);
InputStream in = method.getResponseBodyAsStream();
ServletOutputStream out = response.getOutputStream();
byte buffer[] = new byte[1024 * 128];
int k = 0;
while ((k = in.read(buffer)) != -1) {
out.write(buffer, 0, k);
}
in.close();
out.close();
} catch (Exception e) {
System.out.println("网络连接问题!无需大惊小怪,暂无解决办法!byebye!");
e.printStackTrace();
}
}
private static HttpMethod getMethod(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String relatedtUrl = request.getParameter("RedirectURL");
StringBuffer url = new StringBuffer("/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);
}
}
}
GetMethod get = new GetMethod(url.toString());
get.releaseConnection();
return get;
}
}