最近用Ajax来做页面异步请求,在是在IE8和firefox中没错误,IE6、7都出现了错误,经过两天的奋斗最终确认是因为url长度的问题,由于在页面中拼了一个很长的字符串,并将此字符串当作get请求传送,而url长度很大有超过4000个字节,而ie6、7不支持这样长的url,ie8是可以,这样就导致了无论如何都提交不了
最好的解决方法就是使用POST请求!
代理类如下:
/*
* 文件名:proxyHandler.java
* 描述:
* 修改人:ROGER
* 修改时间:Aug 19, 2011
* 跟踪单号:
* 修改单号:
* 修改内容:
*/
package com.huawei.bjwxcs.common.proxy;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
public class ProxyHandler extends HttpServlet {
private static final long serialVersionUID = 1L;
private final Logger logger = Logger.getLogger(getClass());
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
//System.out.println("------*****---------------------doGet start----------------------------------");
logger.info("------*****---------------------doGet start----------------------------------");
//System.out.println("ProxyHandler noSecurityRequest 请求类型: GET");
String url0 = "";
OutputStream out = null;
HttpURLConnection conn = null;
try {
url0 = req.getQueryString();
if (url0.startsWith("url=")) {
url0 = url0.substring(4);
}
if(url0.indexOf("requestTime")!=-1) {
url0 = url0.split("requestTime")[0];
url0 = url0.substring(0, url0.length()-1);
}
URL url = new URL(url0);
conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
//urlConnection.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
//conn.setConnectTimeout(3000);
out = resp.getOutputStream();
resp.setContentType("text/plain; charset=UTF-8");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), HTTP.UTF_8));
BufferedWriter wd = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream(), HTTP.UTF_8));
String tempLine = rd.readLine();
StringBuffer scenarioSb = new StringBuffer();
while (tempLine != null){
wd.write(tempLine);
scenarioSb.append(tempLine);
tempLine = rd.readLine();
}
wd.flush();
System.out.println("请求地址: "+url0+"请求成功!");
logger.info("请求成功地址: "+url0 +"\\r\\n");
logger.info("返回结果: "+scenarioSb.toString());
//System.out.println("返回结果:"+scenarioSb.toString());
} catch (Exception e) {
logger.info("请求出错地址: "+url0+"错误内容:"+e.getMessage());
} finally {
try {
if(out!=null) {
out.close();
}
if(conn!=null) {
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
logger.info("------*****---------------------doGet end----------------------------------");
//System.out.println("------*****---------------------doGet end----------------------------------");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
logger.info("------*****---------------------doPost start----------------------------------");
String reStr = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), HTTP.UTF_8));
StringBuffer reSb = new StringBuffer();
String tempStr = reader.readLine();
while (tempStr != null){
reSb.append(tempStr);
tempStr = reader.readLine();
}
reStr = reSb.toString();
} catch (Exception ex) {
logger.error("解析客户端发送的请求错误!/n"+ex);
}
String url0 = "";
OutputStream out = null;
HttpURLConnection conn = null;
try {
url0 = req.getQueryString();
if (url0.startsWith("url=")) {
url0 = url0.substring(4);
}
if(url0.indexOf("requestTime")!=-1) {
url0 = url0.split("requestTime")[0];
url0 = url0.substring(0, url0.length()-1);
}
if(reStr!=null){
url0 = url0 + reStr;
}
URL url = new URL(url0);
conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
//urlConnection.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
//conn.setConnectTimeout(3000);
out = resp.getOutputStream();
resp.setContentType("text/plain; charset=UTF-8");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), HTTP.UTF_8));
BufferedWriter wd = new BufferedWriter(new OutputStreamWriter(resp.getOutputStream(), HTTP.UTF_8));
String tempLine = rd.readLine();
StringBuffer scenarioSb = new StringBuffer();
while (tempLine != null){
wd.write(tempLine);
scenarioSb.append(tempLine);
tempLine = rd.readLine();
}
wd.flush();
System.out.println("请求地址: "+url0+"请求成功!");
logger.info("请求成功地址: "+url0 +"\\r\\n");
logger.info("返回结果: "+scenarioSb.toString());
//System.out.println("返回结果:"+scenarioSb.toString());
} catch (Exception e) {
logger.info("请求出错地址: "+url0+"错误内容:"+e.getMessage());
} finally {
try {
if(out!=null) {
out.close();
}
if(conn!=null) {
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
logger.info("------*****---------------------doPost end----------------------------------");
}
}