网络传输的2中方式和重定向 转发的区别

本文介绍了一个Java项目中用于处理HTTP请求的工具类实现,包括POST和GET两种请求方式的具体实现。通过示例代码展示了如何使用该工具类进行JSON数据的下载及远程URL的GET请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.champion.itax.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Snippet {
   public static String postDownloadJson(String path,String post){
           URL url = null;
           try {
               url = new URL(path);
               HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
               httpURLConnection.setRequestMethod("POST");// 提交模式
               // conn.setConnectTimeout(10000);//连接超时 单位毫秒
               // conn.setReadTimeout(2000);//读取超时 单位毫秒
               // 发送POST请求必须设置如下两行
               httpURLConnection.setDoOutput(true);
               httpURLConnection.setDoInput(true);
               // 获取URLConnection对象对应的输出流
               PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
               // 发送请求参数
               printWriter.write(post);//post的参数 xx=xx&yy=yy
               // flush输出流的缓冲
               printWriter.flush();
               //开始获取数据
               BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
               ByteArrayOutputStream bos = new ByteArrayOutputStream();
               int len;
               byte[] arr = new byte[1024];
               while((len=bis.read(arr))!= -1){
                   bos.write(arr,0,len);
                   bos.flush();
               }
               bos.close();
               return bos.toString("utf-8");
           } catch (Exception e) {
               e.printStackTrace();
           }
           return null;
       }
   
   
public static String  restGet(String remoteUrl) throws Exception{
      
      URL getUrl;
      try {
         getUrl = new URL(remoteUrl);
         HttpURLConnection connection = (HttpURLConnection) getUrl .openConnection();    
          connection.connect();  
          BufferedReader reader = new BufferedReader(new InputStreamReader(    
                      connection.getInputStream())); 
          String lines;
               StringBuffer sb = new StringBuffer("");
               while ((lines = reader.readLine()) != null) {
                   lines = new String(lines.getBytes(),"UTF-8");
                   sb.append(lines);
               }  
              reader.close();   
              connection.disconnect(); 
              return sb.toString();
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         throw e;
      }   
   }
   

}


以ajax为例 如果发送验证码 用重定向或转发的方式 则response无法被释放  得到的回调函数会报错

如果不用response的方式 则可能得到一个xml 页面不友好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值