发送请求:

public void testHttp()
{ String result = "";
try
{
URL postURL = new URL("http://localhost:8080/webTest/TestSerlvte");
HttpURLConnection conn = (HttpURLConnection) postURL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
// conn.setConnectTimeout(5 * 1000);
// PUT请求
conn.setRequestMethod("PUT");
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
// json格式上传的模式
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("header1", "header1Text1");
conn.setRequestProperty("header2", "header1Text2"); String payload = "{\"appid\":6,\"appkey\":\"0cf0vGD/ClIrVmvVT/r5hEutH5M=\",\"openid\":200}"; // OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
//解决中文乱码
PrintWriter osw = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));
osw.write(payload);
osw.flush();
osw.close();
System.out.println(conn.getResponseCode());
if (conn.getResponseCode() == 200) {
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inputLine = null;
while ((inputLine = br.readLine()) != null) {
result += inputLine;
}
isr.close();
conn.disconnect();
}
else {
//如果出错,一定要检查URL对没有!
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
String jsontxt = br.readLine();
br.close();
} conn.disconnect(); }
catch (Exception e)
{
System.out.println(e.getMessage());
} }

  

服务端:

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("我是PUT");
req.getHeader("header4");
System.out.println("header4" + "----" + req.getHeader("header4")); ServletInputStream inputStream = req.getInputStream(); StringBuilder content = new StringBuilder();
byte[] b = new byte[req.getContentLength()];
int lens = -1;
while ((lens = inputStream.read(b)) > 0) {
content.append(new String(b, 0, lens));
}
String strcont = content.toString();// 内容 resp.setHeader("resp1", "resp1Text");
super.doPut(req, resp);
}

  

HttpURLConnection 发送PUT请求 json请求体 与服务端接收的更多相关文章

  1. Http学习之使用HttpURLConnection发送post和get请求(3)

    使用HttpURLConnection发送post和get请求 但我们常常会碰到这样一种情况: 通过HttpURLConnection来模拟模拟用户登录Web服务器,服务器使用cookie进行用户认证 ...

  2. Http学习之使用HttpURLConnection发送post和get请求(2)

    接上节Http学习之使用HttpURLConnection发送post和get请求 本节深入学习post请求. 上 节说道,post请求的OutputStream实际上不是网络流,而是写入内存,在ge ...

  3. HttpURLConnection发送GET、POST请求

    HttpURLConnection发送GET.POST请求 /** * GET请求 * * @param requestUrl 请求地址 * @return */ public String get( ...

  4. SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析

    SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析 一:问题demo展示 ...

  5. filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行

    filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行

  6. C# TCP socket发送大数据包时,接收端和发送端数据不一致 服务端接收Receive不完全

    简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的TCP Socket (异步方式) C# 的tcp Socket设置自定义超时时间 C# TCP ...

  7. java httpclient发送json 请求 ,go服务端接收

    /***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...

  8. Http学习之使用HttpURLConnection发送post和get请求(1)

    最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放 ...

  9. ajax跨域请求,页面和java服务端的写法

    方法一(jsonp): 页面ajax请求的写法: $.ajax({ type : "get", async : false, cache : false, url : " ...

随机推荐

  1. Finally语句块的运行

    一.finally语句块是否一定运行? Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被运行?非常多人都说不是.当然他们的回答是正确的,经过试验. ...

  2. Session 钝化机制

  3. SHOW PROCESSLIST Syntax

    https://dev.mysql.com/doc/refman/5.7/en/show-processlist.html SHOW PROCESSLIST shows you which threa ...

  4. 【c++】map 迭代器删除演示样例

    C++ STL中的map是很常见的.通常我们用例如以下方式来遍历,而且删除map中的一些entry: map<int, int> mp; mp.insert(make_pair(1,1)) ...

  5. ES6 一些新特性的总结

    一.箭头函数 ES6中新增了一个箭头函数   ()=>,箭头函数通俗点讲就是匿名函数.箭头函数还有不同点在于改变函数中this,和js中的.bind  的方法差不多,继承后指向的不是最新的函数, ...

  6. 计算机学院大学生程序设计竞赛(2015’12)01 Matrix

    01 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. MYSQL进阶学习笔记二:MySQL存储过程和局部变量!(视频序号:进阶_4-6)

    知识点三:MySQL存储过程和局部变量(4,5,6) 存储过程的创建:     创建存储过程的步骤: 首先选中数据库 改变分隔符,不让分号作为执行结束的标记.(通常情况下,改变分隔符命令 DELIMI ...

  8. Html5--6-46 渐变效果

    Html5--6-46 渐变效果 学习要点 掌握线性渐变和径向渐变的使用 线性渐变: 属性:linear-gradinet(开始位置 角度,起始颜色,终止颜色 ) 开始位置:渐变开始的位置,属性值可以 ...

  9. [yii]Fetch data from database and create listbox in yii

    <?php $records = User::model()->findAll(); $list = CHtml::listData($records, 'id', 'username') ...

  10. spark api之一:Spark官方文档 - 中文翻译

    转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 引入Spark(Linking with Spark) 3 初始化Spark(Initi ...