java发送数据到服务器_java客户端数据发送到服务器(POST请求)总结

java客户端数据发送到服务器(POST请求)总结1.如果不设置Content-type,默认是:application/x-www-form-urlencoded。2.GET请求的参数与对应的值位于请求行中,

今天为了测试服务处理请求的功能,自己学了从客户端发送模拟浏览器发送请求,现在总结如下:

首先看写的相关的代码

客户端主要用到的类是URLConnection

URL url = new URL(":8080/yiliaotest/RetransServlet");

URLConnection con = url.openConnection();

// post请求必须设置下面两项

con.setDoOutput(true);

con.setDoInput(true);

// 不使用缓存

con.setUseCaches(false);

String personjson = "[{\"xingming\":\"namezxc\",\"xingbie\":0}]";

String zhengzhuangjson = "[{\"kesou\":0}]";

// 设置自定义的请求头,也可以用这个方法得到发送数据

con.setRequestProperty("personjson", personjson);

con.setRequestProperty("zhengzhuangjson", zhengzhuangjson);

// 这句是打开链接

OutputStream out = con.getOutputStream();

// 把数据写到报文

out.write(("zhengzhuangjson=" + zhengzhuangjson).getBytes());

// &号是数据间隔,

out.write("&".getBytes());

out.write(("personjson=" + personjson).getBytes());

out.flush();

out.close();

// 这句才是真正发送请求

con.getInputStream();

在doPost方法里写语句

System.out.println("getContentType: " + request.getContentType());

System.out.println("getQueryString: " + request.getQueryString());

System.out.println("getRemoteAddr: " + request.getRemoteAddr());

System.out.println("getRemoteHost: " + request.getRemoteHost());

System.out.println("getRemotePort: " + request.getRemotePort());

System.out.println("getRemoteUser: " + request.getRemoteUser());

System.out.println("getLocalAddr: " + request.getLocalAddr());

System.out.println("getLocalName: " + request.getLocalName());

System.out.println("getLocalPort: " + request.getLocalPort());

System.out.println("getMethod: " + request.getMethod());

System.out.println("-------request.getParamterMap()-------");

// 得到请求的参数Map,,注意map的value是String数组类型

Map map = request.getParameterMap();

Set keySet = map.keySet();

for (String key : keySet) {

String[] values = (String[]) map.get(key);

for (String value : values) {

System.out.println(key);

System.out.println(key + "=" + value);

}

}

System.out.println("-------request.getParamterMap() end-------");

System.out.println("--------request.getHeader()--------");

// 得到请求头的name集合

Enumeration em = request.getHeaderNames();

while (em.hasMoreElements()) {

String name = (String) em.nextElement();

String value = request.getHeader(name);

System.out.println(name + "=" + value);

}

System.out.println("--------request.getHeader() end--------");

// 使用getParameter方法得到请求发来的数据

String personjsonstr = request.getParameter("personjson");

String zhengzhuangstr = request.getParameter("zhengzhuangjson");

System.out.println(personjsonstr);

System.out.println(zhengzhuangstr);

输出结果:

getContentType: application/x-www-form-urlencoded

getQueryString: null

getRemoteAddr: 127.0.0.1

getRemoteHost: 127.0.0.1

getRemotePort: 59236

getRemoteUser: null

getLocalAddr: 127.0.0.1

getLocalName: localhost

getLocalPort: 8080

getMethod: POST

-------request.getParamterMap()-------

zhengzhuangjson

zhengzhuangjson=[{"kesou":0}]

personjson

personjson=[{"xingming":"namezxc","xingbie":0}]

-------request.getParamterMap() end-------

--------request.getHeader()--------

personjson=[{"xingming":"namezxc","xingbie":0}]

zhengzhuangjson=[{"kesou":0}]

cache-control=no-cache

pragma=no-cache

user-agent=Java/1.6.0_65

host=localhost:8080

accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

connection=keep-alive

content-type=application/x-www-form-urlencoded

content-length=77

--------request.getHeader() end--------

[{"xingming":"namezxc","xingbie":0}]

[{"kesou":0}]

1.如果不设置Content-type,默认是:application/x-www-form-urlencoded。

POST请求的数据在请求报文里。

3.两种请求方式的数据都以“key1=value1&key2=value”的格式。

5.对http协议有个好的理解,是学习发送请求、传输数据到服务器的基础。

参考链接:

关于Get与Post请求的区别:

HTTP协议教程:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值