发送请求:

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. LeetCode(3)题解: Longest Palindromic Substring

    https://leetcode.com/problems/longest-palindromic-substring/ 题目: Given a string S, find the longest ...

  2. rpm的gpg key

    1 gpg 这是一种公钥.私钥机制. 2 rpm包的格式 rpm包由四部分构成,lead.signature.header和archive构成. 这里的签名(signature)是加密了的,也就是说, ...

  3. gRPC错误码 http状态码 provide your APIs in both gRPC and RESTful style at the same time

    How gRPC error codes map to HTTP status codes in the response https://github.com/grpc-ecosystem/grpc ...

  4. 20170228 Z_po_send_email

    FUNCTION zmm_po_send_email. function zmm_po_send_email. *"------------------------------------- ...

  5. 单字节的FIFO缓存(30天自制操作系统--读书笔记)

    从今天起,写一些读书笔记.最近几个月都在看<30天自制操作系统这本书>,书虽说看的是电子书,但可以花钱买的正版书,既然花费了金钱,就总得有些收获. 任何人都不能总是固步自封,想要进步就得学 ...

  6. Safair css hack

    一下方式不会影响chrome浏览器样式 _::-webkit-full-page-media, _:future, :root .class{ /*此处放css样式*/ }

  7. [RK3288][Android6.0] 调试笔记 --- 通用GPIO驱动控制LED【转】

    本文转载自:http://m.blog.csdn.net/kris_fei/article/details/69553422 Platform: ROCKCHIPOS: Android 6.0Kern ...

  8. i2c_set_clientdata函数【转】

    本文转载自‘:http://blog.csdn.net/jk198310/article/details/43738367 在i2c驱动中有很多函数和数据结构,很多一时难以理解,所以写下本文共同学习. ...

  9. Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code

    sonarqube的扫描结果提示 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creatin ...

  10. python(一):multiprocessing——死锁

    前言近年来,使用python的人越来越多,这得益于其清晰的语法.低廉的入门代价等因素.尽管python受到的关注日益增多,但python并非完美,例如被人诟病最多的GIL(值得注意的是,GIL并非py ...