日期:2014-05-20  浏览次数:20858 次

代理中遇到的问题。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.IOException"%>
<%@ page import="org.apache.commons.httpclient.HttpClient"%>
<%@ page import="org.apache.commons.httpclient.HttpStatus"%>
<%@ page import="org.apache.commons.httpclient.methods.PostMethod"%>
<%@ page import="org.apache.commons.httpclient.methods.GetMethod"%>
<%@ page import="org.apache.commons.httpclient.methods.RequestEntity"%>
<%@ page import="org.apache.commons.httpclient.params.HttpMethodParams"%>

<%@ page
import="org.apache.commons.httpclient.methods.StringRequestEntity"%>
<%@ page
import="org.apache.commons.httpclient.DefaultHttpMethodRetryHandler"%>
<%@ page import="java.io.DataOutputStream"%>
<%@ page import="org.apache.commons.httpclient.Header"%>

<%@ page import="java.util.Enumeration"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>


<%
  request.setCharacterEncoding("utf-8");  
  response.setContentType("text/html;charset=utf-8");
  //10.243.167.74:8082
  url="http://172.16.90.163:8080/ssoclientapp/loginInforeSys?toModel=INTERFACE&usercode=sa&Params=showContent%3DOPTPATHFIND";
  StringBuffer sb=new StringBuffer();
  HttpClient httpClient = new HttpClient();  
  // Create a method instance.
  PostMethod postMethod = new PostMethod(sb.toString());
  String key = null;
  Enumeration parameterNames = request.getParameterNames();
  while(parameterNames.hasMoreElements()){
  key = (String) parameterNames.nextElement();  
  postMethod.addParameter(key, request.getParameter(key));  
  }try{
  // Execute the method.
  int statusCode = httpClient.executeMethod(postMethod);  
  if (statusCode != HttpStatus.SC_OK)
  {
  out.print("连接管线系统出错 " + postMethod.getStatusLine());
  }  
  // Read the response body.
  byte[] responseBody = postMethod.getResponseBody();  
  //设置返回类型
  Header header = postMethod.getResponseHeader("Content-Type");
  String contentType = header.getValue();  
  response.setContentType(contentType);  
  //判断是否是图片
  if(contentType.indexOf("image") != -1){
  //如果是图片则以字节流的形式返回
  DataOutputStream dos = new DataOutputStream (response.getOutputStream()); 
  dos.write(responseBody, 0, responseBody.length);
  dos.flush();
  dos.close();
  out.clear();
  out=pageContext.pushBody();
  }else{
  //如果是图片以外的格式则以字符串的形式返回
  response.getWriter().write(new String(responseBody,"utf-8"));
  out.clear();
  out=pageContext.pushBody();
  }  
  // out.print(new String(res));