HttpServletRequest 的参数传递问题, 进来看看, 给我解决问题的同时你也能增长一些知识
大家好! 感谢进来帮我解决问题!
问题陈述:
我在使用CA认证遇上一个问题, 由于CA认证的时候要和CA服务器有一个交互.
而且CA服务器限定WEB程序的客户端不能直接和CA服务器交互, 只能有WEB服务端进行交互.所以CA发出验证信息必须通过web服务器做一个中转.
在WEB客户端发出验证的时候(通过访问CA服务器的一个地址, 比如说 http://aaa:8080/a.asp), 我是在WEB服务器段做了一个转发的请求(也就是把http://aaa:8080/a.asp先通过web服务器, 在有web服务器转发出去, 具体看后面的代码), 但是客户端发出请求的时候是带有参数的, 我通过我的方法导致CA服务器那边的参数获取不到.
请大家指点;
代码:
就是把原来请求CA服务器的连接先指向WEB服务器的一个连接
/**
* 处理ceb验证跳转
* pack, unpack, stamp, print
* @param pMapping
* @param pForm
* @param pRequest
* @param pResponse
* @return
* @throws OAException
*/
public ActionForward dealValidateForward(ActionMapping pMapping,
ActionForm pForm, HttpServletRequest pRequest,
HttpServletResponse pResponse) throws OAException{
String operValidate = pRequest.getParameter( "operValidate ");
String cebServerPath = CEBValidateConstant.property.getProperty( "cebServerPath ");
String validatePage = " ";
boolean doOutput = true; //是否需要印章服务器返回数据
if(operValidate.equals( "packValidate ")){
validatePage = cebServerPath + CEBValidateConstant.property.getProperty( "packValidateURL ");
}else if(operValidate.equals( "unPackValidate ")){
validatePage = cebServerPath + CEBValidateConstant.property.getProperty( "unpackValidateURL ");
}else if(operValidate.equals( "stampValidate ")){
validatePage = cebServerPath + CEBValidateConstant.property.getProperty( "stampValidateURL ");
}else{
validatePage = cebServerPath + CEBValidateConstant.property.getProperty( "printValidateURL ");
}
try {
CEBValidateConstant.invokeRemote(validatePage, doOutput, pRequest,
pResponse);
} catch (Exception e) {
e.getMessage();
throw new OAException( "印章校验出错! ");
}
return null;
}
下面的方法是转发数据包, 和获取返回的数据
/**
* 访问印章服务器
* @param url 要访问的地址
* @param doOutput 是否需要印章服务器返回信息
* @param request HttpServletRequest请求包
* @param response HttpServletResponse响应包
* @return string 被访问的服务器的返回信息
* @throws IOException 如果连接印章服务器失败则抛出IOException
* @throws MalformedURLException
*/
public static String invokeRemote(String url,boolean doOutput,HttpServletRequest request,HttpServletResponse response) throws IOException,MalformedURLException{
String retVal = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
URL server = new URL(url);
HttpURLConnection conn = (HttpURLConnection)server.openConnection();
conn.setDoOutput(doOutput);
conn.setDoInput(true);
conn.setUseCaches(false);
//weicd
conn.setRequestMethod( "POST ");
conn.setRequestProperty( "Cache-Control ", "no-cache ");
DataInputStream in = null;
if(doOutput){//需要接收客户端数据包
//读取包
in = new DataInputStream(new BufferedInputStream(request.getInputStream()));
int ch = in.read();
while(ch!=-1){
baos.write(ch);
ch = in.read();
}
in.close();
//System.out.println(baos.toString());
byte[] content = baos.toByteArray();
baos.close();
//转发数据包
DataOutputStream outToStampServer = new DataOutputStream(new BufferedOutputStream(conn.getOutputStream()));
outToStampServer.write(content);
outToStampServer.flush();
outToStampServer.close();
int response_code = conn.getResponseCode();
System.out.println( "response_code " + response_code);
System.out.println( "conn.getResponseMessage() " + conn.getResponseMessage());
System.out.println( "conn.getContentLength() " + conn.getContentLength());
System.out.println( "conn.getContentType() " + conn.getContentType());
/*log( "CebSeal.java 272 response code: "+response_code);
log( "CebSeal.java 273 response message: "+conn.getResponseMessage());
log( "CebSeal.java 274 response Length: "+conn.getContentLength());
log( "CebSeal.java 275 response contentType: "+conn.getContentType());*/
}
//读取公章服务器的执行结果
in = new DataInputStream(new BufferedInputStream(conn.getInputStream()));
int ch = -1;
ch = in.read();
while(ch!=-1){
baos.write(ch);
ch = in.read();
}
in.close();
byte[] content = baos.toByteArray();
baos.close();
retVal = new String(content);
System.out.println( "返回值: " + retVal);
if(doOutput){
DataOutputStream outToClient = new DataOutputStream(response.getOutputStream());
outToClient.write(content);
}
conn.disconnect();
return retVal;
/*String createID = request.getParameter( "CreateID ");
String issueID = request.getParameter( "IssueID ");
request.getAttributeNames();
Map reqMap = request.getParameterMap();
String paramIn = " ";
for (Iterator iter = reqMap.keySet().iterator(); iter.hasNext();) {
String param = (String) iter.next();
String paramStr = param + "= "
+ ((String[]) reqMap.get(param))[0];
paramIn = paramIn + paramStr + "& ";
}
String s1=sendPost(url,paramIn);
return s1;*/
}//invokeRemote()
问题就是通过这种方式, 导致CA服务器那边有的参数获取不到, 但是客户端直接连CA服务器验证是好的, 请大家帮帮忙....
[解决办法]
昏,还以为是个小问题,那么多头都大了~
[解决办法]
参数包含中文吗?是不是编码的问题