setrequestproperty参数_HttpURLConnection的addRequestProperty和setRequestProperty「建议收藏」

setrequestproperty参数_HttpURLConnection的addRequestProperty和setRequestProperty「建议收藏」一、当只有addRequestProperty的时候URLurl=newURL(“http://localhost:8080/net/listnets.jsp”);URLConnectionconnection=url.openConnection();connection.addRequestProperty(“name”,”asad”);connection.addReques…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

一、当只有addRequestProperty的时候

URL url = new URL(“http://localhost:8080/net/listnets.jsp”);

URLConnection connection = url.openConnection();

connection.addRequestProperty(“name”, “asad”);

connection.addRequestProperty(“name”, “komal”);

connection.addRequestProperty(“class”, “10th”);

connection.addRequestProperty(“Address”, “Delhi 17”);

Map map = connection.getRequestProperties();

Set set = map.entrySet();

Iterator iterator = set.iterator();

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

输出结果:name=[komal, asad]

Address=[Delhi 17]

class=[10th]

二、当只有setRequestProperty的时候

URL url = new URL(“http://localhost:8080/net/listnets.jsp”);

URLConnection connection = url.openConnection();

connection.setRequestProperty(“name”, “komal”);

connection.setRequestProperty(“name”, “asad”);

connection.setRequestProperty(“class”, “10th”);

connection.setRequestProperty(“Address”, “Delhi 17”);

Map map = connection.getRequestProperties();

Set set = map.entrySet();

Iterator iterator = set.iterator();

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

输出结果:

name=[asad]

Address=[Delhi 17]

class=[10th]

注意name的设置,会发生覆盖的作用。

三、当先set后add的时候

URL url = new URL(“http://localhost:8080/net/listnets.jsp”);

URLConnection connection = url.openConnection();

connection.setRequestProperty(“name”, “asad”);

connection.addRequestProperty(“name”, “komal”);

connection.addRequestProperty(“class”, “10th”);

connection.addRequestProperty(“Address”, “Delhi 17”);

Map map = connection.getRequestProperties();

Set set = map.entrySet();

Iterator iterator = set.iterator();

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

输出结果

name=[komal, asad]

Address=[Delhi 17]

class=[10th]

四、当先add后set的时候

URL url = new URL(“http://localhost:8080/net/listnets.jsp”);

URLConnection connection = url.openConnection();

connection.addRequestProperty(“name”, “komal”);

connection.setRequestProperty(“name”, “asad”);

connection.addRequestProperty(“class”, “10th”);

connection.addRequestProperty(“Address”, “Delhi 17”);

Map map = connection.getRequestProperties();

Set set = map.entrySet();

Iterator iterator = set.iterator();

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

name=[asad]

Address=[Delhi 17]

class=[10th]

五、结

setRequestProperty方法,如果key存在,则覆盖;不存在,直接添加。

addRequestProperty方法,不管key存在不存在,直接添加。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195173.html原文链接:https://javaforall.net

(0)
上一篇 2025年10月22日 下午2:22
下一篇 2025年10月22日 下午3:01


相关推荐

  • 【综合帖】Java并发多线程编程学习专栏

    本帖主要用于记录Java多线程或并发编程的一些学习书籍和一些优秀的博文,方便自己学习并发多线程编程的知识!也为了通过自己的学习整理出学习笔记,输出一个java并发编程的专栏。让自己的学习的知识沉淀下来!本篇记录的博文或者些图片内容都来自互联网,如果有任何侵权或者疑问,可以联系我!

    2022年2月26日
    51
  • Spring-boot_Spring Boot

    Spring-boot_Spring Boot1概述Jasypt是一个加密库,Github上有一个集成了Jasypt的SpringBoot库,叫jasypt-spring-boot,本文演示了如何使用该库对配置文件进行加密。2依赖首先添加依赖:<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId>

    2026年4月14日
    5
  • Memcached和Redis区别

    Memcached和Redis区别多线程 锁 memcached vs 单线程 多路 IO 复用 Redis 与 Memcache 三点不同 支持多数据类型 支持持久化 单线程 多路 IO 复用

    2026年3月16日
    2
  • 1.如何实现MT4帐号同步交易?

    1.如何实现MT4帐号同步交易?使用本跟单EA,可以实现在同一台计算机上运行两个(或更多个)MetaTrader4自动复制交易。您使用其中一个MT4帐号手动交易或者使用EA自动交易,那么其它一个(或更多个)MT4,会立即复制此帐号中的订单。您可以运行多个喊单EA和多个跟单EA,可以实现一个帐号跟多个帐号,或者多个帐号跟一个帐号,又或者多个帐号跟多个帐号。用来喊单的MT4帐号不需要帐号必须拥交易权限,因此,可以使用MT4“投资者”密码登录。投资者密码,又称呼“只读密码、观摩密码”。…

    2022年5月30日
    40
  • shell编程判断100以内所有素数(质数)

    shell编程判断100以内所有素数(质数)echo n pleaseentern readndeclare Iafor i 1 i lt n i dofor x 1 x lt i x dob i x if b eq0 thena a 1fidoneif a

    2026年3月19日
    2
  • JS数组的定义

    JS数组的定义

    2021年8月25日
    59

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号