HTTP各种请求方式(get,post,delete,put)

本文详细介绍了HTTP中四种主要的请求方法:POST用于提交数据,GET用于获取资源,DELETE用于删除资源,PUT用于更新资源。理解这些方法在Web开发中的应用至关重要。

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

1.POST方式

	public static void main(String[] args) throws UnsupportedEncodingException, IOException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ceshi", "1");
		Map<String, String> list=new HashMap<String, String>();
		list.put("date",token);
		String RepairListForApp = httpPost("http://",list, map);
		System.out.println(RepairListForApp);
    }
public static String httpPost(String url,Map<String, String> header,Map map){
        // 返回body
        String body = null;
        // 获取连接客户端工具
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse httpResponse=null;
        // 2、创建一个HttpPost请求
        HttpPost post = new HttpPost(url);
        // 5、设置header信息
		/**header中通用属性*/
		post.setHeader("Accept","*/*");
		post.setHeader("Accept-Encoding","gzip, deflate");
		post.setHeader("Cache-Control","no-cache");
		post.setHeader("Connection", "keep-alive");
		post.setHeader("Content-Type", "application/json;charset=UTF-8");
		/**业务参数*/
		if (header!=null) {
            Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String, String> entry = it.next();
                System.out.println(entry.getKey()+":"+entry.getValue());
                post.setHeader(entry.getKey(), entry.getValue());
            }
        }
        // 设置参数
        if (map != null) {
            try {
                StringEntity entity1=new StringEntity(JSON.toJSONString(map),"UTF-8");
                entity1.setContentEncoding("UTF-8");
                entity1.setContentType("application/json");
                post.setEntity(entity1);

                // 7、执行post请求操作,并拿到结果
                httpResponse = httpClient.execute(post);
                // 获取结果实体
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null) {
                    // 按指定编码转换结果实体为String类型
                    body = EntityUtils.toString(entity, "UTF-8");
                }
                try {
                    httpResponse.close();
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return body;
}

2.get方式

	public static void main(String[] args) throws UnsupportedEncodingException, IOException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ceshi", "1");
		Map<String, String> list=new HashMap<String, String>();
		list.put("date",token);
		String RepairListForApp = httpGet("http://", list);
		System.out.println(RepairListForApp);
    }
public static String httpGet(String url, Map<String, String> header){
	    // 获取连接客户端工具
	    CloseableHttpClient httpClient = HttpClients.createDefault();
	    CloseableHttpResponse httpResponse=null;
	    String finalString = null;
	    HttpGet httpGet = new HttpGet(url);
	    /**公共参数添加至httpGet*/
		/**header中通用属性*/
		httpGet.setHeader("Accept","*/*");
		httpGet.setHeader("Accept-Encoding","gzip, deflate");
		httpGet.setHeader("Cache-Control","no-cache");
		httpGet.setHeader("Connection", "keep-alive");
		httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
		/**业务参数*/
		if (header!=null) {
            Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String, String> entry = it.next();
                System.out.println(entry.getKey()+":"+entry.getValue());
                httpGet.setHeader(entry.getKey(), entry.getValue());
            }
        }
	    try {
	        httpResponse = httpClient.execute(httpGet);
	        HttpEntity entity = httpResponse.getEntity();
	        finalString= EntityUtils.toString(entity, "UTF-8");
	        try {
	            httpResponse.close();
	            httpClient.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return finalString;
	}

3.delete方式

	public static void main(String[] args) throws UnsupportedEncodingException, IOException{
		Map<String, String> list=new HashMap<String, String>();
		list.put("date",token);
		String RepairListForApp = httpdel("http://", list,null);
		System.out.println(RepairListForApp);
    }
public static String httpdel(String url,Map<String, String> header,Map map){
		// 返回body
		String body = null;
		// 获取连接客户端工具
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse=null;
		 String finalString = null;
		// 2、创建一个HttpPost请求
		HttpDelete httpGet = new HttpDelete(url);
	    /**公共参数添加至httpGet*/
		/**header中通用属性*/
		httpGet.setHeader("Accept","*/*");
		httpGet.setHeader("Accept-Encoding","gzip, deflate");
		httpGet.setHeader("Cache-Control","no-cache");
		httpGet.setHeader("Connection", "keep-alive");
		httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
		/**业务参数*/
		if (header!=null) {
            Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String, String> entry = it.next();
                System.out.println(entry.getKey()+":"+entry.getValue());
                httpGet.setHeader(entry.getKey(), entry.getValue());
            }
        }
		if(map!=null){
			StringEntity entity1=new StringEntity(JSON.toJSONString(map),"UTF-8");
			((HttpResponse) httpGet).setEntity(entity1);
		}
	    try {
	        httpResponse = httpClient.execute(httpGet);
	        HttpEntity entity = httpResponse.getEntity();
	        finalString= EntityUtils.toString(entity, "UTF-8");
	        try {
	            httpResponse.close();
	            httpClient.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return finalString;
	}

4.put方式

	public static void main(String[] args) throws UnsupportedEncodingException, IOException{
		Map<String, String> list=new HashMap<String, String>();
		list.put("date",token);
		String RepairListForApp = httpPut("http://", list,null);
		System.out.println(RepairListForApp);
    }
public static String httpPut(String url,Map<String, String> header,Map map){
		// 返回body
		String body = null;
		// 获取连接客户端工具
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse httpResponse=null;
		String finalString = null;
		// 2、创建一个HttpPost请求
		HttpPut httpGet = new HttpPut(url);
		/**公共参数添加至httpGet*/
		/**header中通用属性*/
		httpGet.setHeader("Accept","*/*");
		httpGet.setHeader("Accept-Encoding","gzip, deflate");
		httpGet.setHeader("Cache-Control","no-cache");
		httpGet.setHeader("Connection", "keep-alive");
		httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
		/**业务参数*/
		if (header!=null) {
			Iterator<Map.Entry<String, String>> it =header.entrySet().iterator();
			while(it.hasNext()){
				Map.Entry<String, String> entry = it.next();
				System.out.println(entry.getKey()+":"+entry.getValue());
				httpGet.setHeader(entry.getKey(), entry.getValue());
			}
		}
		if(map!=null){
			StringEntity entity1=new StringEntity(JSON.toJSONString(map),"UTF-8");
			httpGet.setEntity(entity1);
		}
		try {
			httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			finalString= EntityUtils.toString(entity, "UTF-8");
			try {
				httpResponse.close();
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return finalString;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java_才怪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值