求教java中怎么java实现get请求http的patch方法的请求

& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区1094人阅读
RESTFul(1)
我们在调用RESTFul服务的时候,有的时候,第三方的服务会提供支持PATCH 操作的方法,在这种情况下,我们如果我们下面的这种方式,
去调用PATCH操作,肯定会返回40X的错误。
PATCH /tasks/v1/users/@me/lists/MTIxNzM1OTkzNjgxNTQ1OTcwNTc6MDow HTTP/1.1
content-type: application/ charset=UTF-8
connection: close
image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
authorization: Bearer ya29.LgALd08g5-NIQxwAAAB111tdMC-Q-jXhMoRxYEUS107_bD9p6xdVEq9tht3dpQ
accept-encoding:
iso-8859-1,*,utf-8
x-http-method-override: PATCH
accept-charset: utf-8
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: :443
Content-Length: 45
&title&:&This is a test for patch 5555&
那么,解决的方案是什么呢?发送的时候,我们还是用POST方法,只不过我们在HTTP的消息头里面加上一个属性:x-http-method-override
然后把这个属性的值设置成“PATCH”,然后就可以了,请看下面的测试的请求和结果。
(1)测试请求
(2) 测试返回的结果,说明Patch操作的修改成功了!!!
&kind&: &tasks#taskList&,
&id&: &MTIxNzM1OTkzNjgxNTQ1OTcwNTc6MDow&,
&etag&: &\&esSgSc9hKkvJ92xs_2jT5Am6L4c/KKY36xrOZKKq3hKD4zW4yCwNmJo\&&,
&title&: &This is a test for patch 5555&,
&updated&: &T08:31:24.000Z&,
&selfLink&: &/tasks/v1/users/@me/lists/MTIxNzM1OTkzNjgxNTQ1OTcwNTc6MDow&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:80194次
积分:1929
积分:1929
排名:第13981名
原创:99篇
评论:46条
(6)(4)(3)(2)(14)(13)(12)(9)(1)(23)(9)(7)(2)(2)JAVA中利用socket实现HTTP请求
一个基于java的web服务器实现主要使用这两个重要的类:java.net.Socket和java.net.ServerSocket,并通过HTTP消息进行通信。
一个HTTP请求包括三个组成部分:
1.方法&统一资源标示符(URI)&协议/版本
2.请求的头部
3.主体内容
GET / HTTP/1.1 &
Host: 127.0.0.1:8080 &
Connection: keep-alive &
Accept: text/html,application/xhtml+xml,application/q=0.9,image/webp,*/*;q=0.8 &
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0. Safari/537.36 &
Accept-Encoding: gzip,deflate,sdch &
Accept-Language: zh-CN,q=0.8 &
类似于HTTP请求,一个HTTP响应也包括三个组成部分:
1.方法-统一资源标示符(URI)-协议/版本
2.响应的头部
3.主体内容
下面是一个HTTP响应的例子:
[plain] view plaincopyprint?
HTTP/1.1 200 OK & &
Server: IBM/4.0 & &
Date: Sat, 6 Nov :00 GMT & &
Content-Type: text/html & &
Last-Modified: Sat, 5 Jan :12 GMT & &
Content-Length: 112 & &
套接字是网络连接的一个端点。套接字使得一个应用可以从网络中读取和写入数据。放在两个不同的计算机上的两个应用可以通过连接发送和接受字节流。为了从你的应用发送一条信息到另一个应用,你需要指导另一个应用的IP地址和套接字端口。
ServerSocket类
Socket类代表一个客户端套接字,即任何时候你想连接到一个远程服务器应用的时候你构造的套接字。如果是一个服务器程序依靠Socket类是行不通的。你的服务器必须随时待命,因为客户端何时想你发送请求是不知道的。
ServerSocket和Socket不同,服务器套接字的角色是等待来自客户端的连接请求。一旦服务器套接字获得一个连接请求,它创建一个Socket实例来与客户端进行通信。
下面是一个简单的HTTP请求的实现:
先创建一个HTTPSERVER服务
public class HttpServer { &
& & public static final String WEB_ROOT = &&d:/webroot&; &
& & private static final String SHUTDOWN_COMMAND = &/SHUTDOWN&; &
& & private boolean shutdown = &
& & public static void main(String[] args) { &
& & & & HttpServer server = new HttpServer(); &
& & & & server.await(); &
& & public void await() { &
& & & & ServerSocket serverSocket = &
& & & & int port = 8080; &
& & & & try { &
& & & & & & serverSocket = new ServerSocket(port, 1, InetAddress &
& & & & & & & & & & .getByName(&127.0.0.1&)); &
& & & & } catch (UnknownHostException e) { &
& & & & & & e.printStackTrace(); &
& & & & } catch (IOException e) { &
& & & & & & e.printStackTrace(); &
& & & & & & System.exit(1); &
& & & & } &
& & & & // Loop waiting for a request &
& & & & while (!shutdown) { &
& & & & & & Socket socket = &
& & & & & & InputStream input = &
& & & & & & OutputStream output = &
& & & & & & try { &
& & & & & & & & socket = serverSocket.accept(); &
& & & & & & & & input = socket.getInputStream(); &
& & & & & & & & output = socket.getOutputStream(); &
& & & & & & & & // create Request object and parse &
& & & & & & & & Request request = new Request(input); &
& & & & & & & & request.parse(); &
& & & & & & & & // create Response object &
& & & & & & & & Response response = new Response(output); &
& & & & & & & & response.setRequest(request); &
& & & & & & & & response.sendStaticResource(); &
& & & & & & & & // C &
& & & & & & & & socket.close(); &
& & & & & & & & // check if the revious URI is a shutdown command &
& & & & & & & & shutdown = request.getUri().equals(SHUTDOWN_COMMAND); &
& & & & & & } catch (IOException e) { &
& & & & & & & & e.printStackTrace(); &
& & & & & & & & &
& & & & & & } &
& & & & } &
对应服务要有请求
public class Request { &
& & private InputS &
& & private S &
& & public Request(InputStream input) { &
& & & & this.input = &
& & public void parse() { &
& & & & // Read a set of characters from the socket &
& & & & StringBuffer request = new StringBuffer(2018); &
& & & & byte[] buffer = new byte[2048]; &
& & & & try { &
& & & & & & i = input.read(buffer); &
& & & & } catch (IOException e) { &
& & & & & & e.printStackTrace(); &
& & & & & & i = -1; &
& & & & } &
& & & & for (int j = 0; j & j ++) { &
& & & & & & request.append((char)buffer[j]); &
& & & & } &
& & & & System.out.println(&-----------------request----------------&); &
& & & & System.out.print(request.toString()); &
& & & & uri = parseUri(request.toString()); &
& & private String parseUri(String requestString) { &
& & & & int index1, index2; &
& & & & index1 = requestString.indexOf(' '); &
& & & & if (index1 != -1) { &
& & & & & & index2 = requestString.indexOf(' ', index1 + 1); &
& & & & & & return requestString.substring(index1 + 1, index2); &
& & & & } &
& & public String getUri() { &
对应请求要有响应
public class Response { &
& & private static final int BUFFER_SIZE = 1024; &
& & private R &
& & private OutputS &
& & public Response(OutputStream output) { &
& & & & this.output = &
& & public void setRequest(Request request) { &
& & & & this.request = &
& & public void sendStaticResource() throws IOException { &
& & & & byte[] bytes = new byte[BUFFER_SIZE]; &
& & & & FileInputStream fis = &
& & & & boolean errorFlag = &
& & & & if(request.getUri()!=null){ &
& & & & & & File file = new File(HttpServer.WEB_ROOT, request.getUri()); &
& & & & & & if (file.exists()) { &
& & & & & & & & fis = new FileInputStream(file); &
& & & & & & & & int ch = fis.read(bytes, 0, BUFFER_SIZE); &
& & & & & & & & while (ch != -1) { &
& & & & & & & & & & output.write(bytes, 0, ch); &
& & & & & & & & & & ch = fis.read(bytes, 0, BUFFER_SIZE); &
& & & & & & & & } &
& & & & & & & & errorFlag = &
& & & & & & } &
& & & & } &
& & & & if(errorFlag) { &
& & & & & & // file not found &
& & & & & & String errorMessage = &HTTP/1.1 404 File NOT Fount\r\n& + &
& & & & & & & & & & &Content-Type: text/html\r\n& + &
& & & & & & & & & & &Content-Length: 23\r\n& + &
& & & & & & & & & & &\r\n& + &
& & & & & & & & & & &&h1&File Not Found&/h1&&; &
& & & & & & output.write(errorMessage.getBytes()); &
& & & & } &
& & & & if (fis != null) { &
& & & & & & fis.close(); &
& & & & } &
以上三个简单的类创建好后,我们可以启动HttpServer,然后在上输入127.0.0.1:8080 可以看见一个File Not Found,如果想有结果输出到页面上,请配置一个文件在webroot下,例如建一个order.txt,在里面写些内容,也可以弄一个,这样就可以在页面上显示你所要看的页面,请求方式就是http://127.0.0.1:8080/order.txt&
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'君,已阅读到文档的结尾了呢~~
基于android的汽车体检系统设计..
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
基于android的汽车体检系统设计与实现
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口spring的3.1.0版本没有patch请求吗?_java吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:449,017贴子:
spring的3.1.0版本没有patch请求吗?收藏
本人做springmvc
为什么RequestMethod里面没patch请求方式呢
是版本太低吗
what is patch
用的4.0,里面只有get
delete是你记错了吧。。
一辆宝马在拐弯,忽然走出一位大妈在车旁倒下,司机吓白了脸,我看见这一幕,义愤填膺,掏出100元,往大妈前面一扔:“大妈,你的钱掉了!”大妈立马站起来,把钱捡走了。司机看着大妈的背影,感动的对我说:“真多亏你了!”说完掏出500元硬塞给我。看着远去的宝马,我掏出手机:“喂,奶奶,下一个路口……”
get post 还有什么patch?
options put delete get post trace head
其中并没有叫patch的
膜拜大神了。
懂得一种东西叫百度
还真有,都没用过,我也不知道
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或

我要回帖

更多关于 java patch请求 的文章

 

随机推荐