关于java response的两种页面输出方式,以及HttpUrlconnection 代理使用注意点

String="www.baidu.com"  //代理百度

URL url=new URL(connUrl);  //创建url对象

HttpURLConnection conn=(HttpURLConnection)url.openConnection(); //创建连接对象

onn.setRequestProperty("Content-Type","text/html; charset=UTF-8"); //设置传递的编码方式


conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

conn.setRequestMethod("POST");//post提交方式

conn.setDoInput(true);

conn.setDoOutput(true);

 

reponse.getWriter();//以字符流输出,代理中有可能出现编码问题

reponse.getOutputStream();以字节流输出

 

 ---------------------------------------------------------------------------------------------下面为代码---------------------------------------------------------------------------------------------

 

//设置post和get方法

private static final String SERVER_POST="POST";
private static final String SERVLET_GET = "GET";

//post访问方法
 public  static HttpURLConnection doPost(HttpServletRequest request, HttpServletResponse response,String connUrl) throws IOException{
   Enumeration<String> e=  request.getParameterNames();
   StringBuffer param=new StringBuffer();

//获取页面表单信息
     while(e.hasMoreElements())
          {
              String name=(String)e.nextElement();
              String namepar[]=request.getParameterValues(name);
              for(int i=0;i<namepar.length;i++)
                   {
                param.append(name).append("=").append(namepar[i]).append("&"); 
                   }
          }
     if(param.length()!=0){
       param.deleteCharAt(param.length()-1);//去除最后一个&符号
     }
     URL url=new URL(connUrl);
   HttpURLConnection conn=(HttpURLConnection)url.openConnection();
   conn.setRequestProperty("Content-Type","text/html; charset=UTF-8"); 
   conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
   conn.setRequestMethod(SERVER_POST);
   conn.setDoInput(true);
   conn.setDoOutput(true);
   conn.connect(); 

//往代理服务端传递信息
   DataOutputStream out = new DataOutputStream(conn.getOutputStream());
   out.write(param.toString().getBytes("utf-8"));
   out.flush();
   out.close();
   return conn;
 }

 

//get访问方法
 public  static HttpURLConnection doGet(HttpServletRequest request, HttpServletResponse response,String connUrl) throws IOException{
  Enumeration<String> e=  request.getParameterNames();
  StringBuffer param=new StringBuffer();
    while(e.hasMoreElements())
         {
             String name=(String)e.nextElement();
             String namepar[]=request.getParameterValues(name);
             for(int i=0;i<namepar.length;i++)
                  {
               param.append(name).append("=").append(namepar[i]).append("&"); 
                  }
         }
    if(param.length()!=0){
      param.deleteCharAt(param.length()-1);
    }
    URL url=new URL(connUrl+"?"+param.toString());
   HttpURLConnection conn=(HttpURLConnection)url.openConnection();
   conn.setRequestMethod(SERVLET_GET); 
   conn.setRequestProperty("Content-Type","text/html; charset=UTF-8"); 
   conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
   conn.connect(); 
   return conn;
 }

 

//执行方法,传入request和response,以及访问的Url
 public static byte[] exec(HttpServletRequest request, HttpServletResponse response,String connUrl) throws Exception{ 
  HttpURLConnection conn=null;
  if(request.getMethod().equalsIgnoreCase(SERVER_POST)){
    conn=doPost(request, response,connUrl);
  }
  else{
    conn=doGet(request, response,connUrl);
  }
  byte[] b= readInputStream(conn.getInputStream());
  response.getOutputStream().write(b);
  response.getOutputStream().flush();
  response.getOutputStream().close();
  conn.disconnect();
     return b;
 }
 
 //读取输入流
 public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len = inStream.read(buffer)) !=-1 ){
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();//形成网页的流
        outStream.close();
        inStream.close();
        return data;
    }
 

//例子
 public static void main(String[] args) throws Exception {
  HttpServletRequest request = null;
  HttpServletResponse response = null;
  String connUrl="www.baidu.com";
  exec(request,response,connUrl);
 }

 

 

原版百度搜索,和代理版百度搜索

 

由于传入参数有编码格式的要求,此方法为get的方法,所以只能用英文,如果想用中文索索,请使用post的提交方式,

 

 

 

最近在做java项目,做此笔记,造福程旭媛----------------------------------------------------爱神酷星(萧星)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值