Java HttpUtils类

Java HttpUtils类

Java HttpUtils类

定义

Public class HttpUtils

收集HTTP Servlet使用的静态的有效方法。

方法

1、getRequestURL
public static StringBuffer getRequestURL(HttpServletRequest request);

在服务器上重建客户端用来建立请求的URL。这个方法反映了不同的协议(例如http和https)和端口,但不包含查询字符串。这个方法返回一个StringBuffer而不是一个String,这样URL可以被Servlet开发者有效地修改。

2、parsePostData
public static Hashtable parsePostData(int len, ServletInputstream in);

解析一个包含MIME类型application/x-www-form-urlencoded的数据的流,并创建一个具有关键值-数据对的 hash table。这里的关键值是字符串,数据是该字符串所对应的值的列表。一个关键值可以在POST的数据中出现一次或多次。这个关键值每出现一次,它的相应的值就被加入到hash table中的字符串所对应的值的列表中。
从POST数据读出的数据将经过URL解码,+将被转换为空格以十六进制传送的数据(例如%xx)将被转换成字符。
当POST数据无效时,该方法抛出一个IllegalArgumentException。

3、parseQueryString
public static Hashtable parseQueryString(String s);
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java实现的HTTP工具,支持发送GET、POST请求,以及GET请求带请求体: ```java import java.io.*; import java.net.*; public class HttpUtils { public static String sendGet(String url, String params) throws Exception { String fullUrl = url; if (params != null && !params.isEmpty()) { fullUrl += "?" + params; } URL obj = new URL(fullUrl); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } public static String sendPost(String url, String params) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); con.setDoOutput(true); if (params != null && !params.isEmpty()) { OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); writer.write(params); writer.flush(); writer.close(); } int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } public static String sendGetWithBody(String url, String params) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); con.setDoOutput(true); if (params != null && !params.isEmpty()) { OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); writer.write(params); writer.flush(); writer.close(); } int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } } ``` 这个工具包含了三个方法:sendGet、sendPost、sendGetWithBody,分别用于发送GET请求、POST请求,以及GET请求带请求体。其中,sendGet和sendPost方法与常见的HTTP工具似,而sendGetWithBody方法则是采用POST请求来模拟GET请求带请求体的情况。需要注意的是,在使用sendGetWithBody方法时,需要将请求方法设置为POST,并在请求体中传递参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值