xml 你怎么做一个HTTP Put?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/812711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you do an HTTP Put?
提问by Ronnie Overby
We have this software that has a webservices component.
我们有这个具有网络服务组件的软件。
Now, the administrator of this system has come to me, wanting to import data into the system by using the webservices component.
现在,这个系统的管理员来找我,想通过webservices组件将数据导入系统。
So, I went to read the documentation to try to figure this thing out and I am seeing things like this:
所以,我去阅读文档以试图弄清楚这件事,我看到了这样的事情:
That documentation gives examples of interacting with the system using HTTP verbs such as GET, POST, PUT, DELETE. But in my limited experience, I have never had to send neither an HTTP PUT nor a DELETE.
该文档提供了使用 HTTP 动词(如 GET、POST、PUT、DELETE)与系统交互的示例。但以我有限的经验,我从来不需要发送 HTTP PUT 或 DELETE。
How do you do it? I have built HTML forms that have method="post" or method="get" and the request is sent to whatever is specified in the action attribute (action="someResource"). But I don't really know what to do with this PUT thing.
你怎么做呢?我已经构建了具有 method="post" 或 method="get" 的 HTML 表单,并且请求被发送到 action 属性中指定的任何内容 (action="someResource")。但我真的不知道如何处理这个 PUT 的东西。
If I had to guess, I would have to build an application that creates some sort of an HTTP Request object and set all the properties of it and somehow include the data I want to PUT to the RESOURCE (
如果我不得不猜测,我将不得不构建一个应用程序来创建某种类型的 HTTP 请求对象并设置它的所有属性,并以某种方式包含我想要放入 RESOURCE 的数据(
I thought that I was a web developer, since I know things like XHTML, CSS, JavaScript, etc. but it's starting to look like I don't know anything about the foundations of the web at all (HTTP).
我以为我是一名 Web 开发人员,因为我知道 XHTML、CSS、JavaScript 等,但现在看来我对 Web 的基础(HTTP)一无所知。
EDIT
编辑
PS: I program mostly with .net. So, any examples in .net would be pretty awesome.
PS:我主要用 .net 编程。因此,.net 中的任何示例都非常棒。
采纳答案by Jason DeFontes
Here's a C# example using HttpWebRequest:
这是一个使用 HttpWebRequest 的 C# 示例:
using System;
using System.IO;
using System.Net;
class Test
{
static void Main()
{
string xml = "<xml>...</xml>";
byte[] arr = System.Text.Encoding.UTF8.GetBytes(xml);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/");
request.Method = "PUT";
request.ContentType = "text/xml";
request.ContentLength = arr.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(arr, 0, arr.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string returnString = response.StatusCode.ToString();
Console.WriteLine(returnString);
}
}
Update: there's now an HttpClient class in System.Net.Http (available as a NuGet package) that makes this a bit easier:
更新:现在 System.Net.Http 中有一个 HttpClient 类(可作为 NuGet 包使用),这使得这更容易一些:
using System;
using System.Net.Http;
class Program
{
static void Main()
{
var client = new HttpClient();
var content = new StringContent("<xml>...</xml>");
var response = client.PutAsync("http://localhost/", content).Result;
Console.WriteLine(response.StatusCode);
}
}
回答by tvanfosson
PUT and DELETE are likely to require that you use AJAX and make XMLHttpRequests since the FORM tag only supports GET and POST verbs and links only make GET requests.
PUT 和 DELETE 可能要求您使用 AJAX 并发出 XMLHttpRequests,因为 FORM 标记仅支持 GET 和 POST 动词并且链接仅发出 GET 请求。
With jQuery:
使用 jQuery:
$.ajax( {
url: '/controller/action',
type: 'PUT',
data: function() { ...package some data as XML },
dataType: 'xml',
... more options...
);
The note on the jQuery ajax optionspage warns that some browsers don't support PUT and DELETE for the request type. FWIW, I've never used PUT but have used DELETE in IE and FF. Haven't tested in Safari or Opera.
jQuery ajax 选项页面上的注释警告某些浏览器不支持请求类型的 PUT 和 DELETE。FWIW,我从未使用过 PUT,但在 IE 和 FF 中使用过 DELETE。尚未在 Safari 或 Opera 中测试。
回答by Tony
Here is how to do it in CURL: How to Use cURL to Test RESTful Rails
以下是在 CURL 中的操作方法:How to Use cURL to Test RESTful Rails
Or...you can definitely use an HTML form. If the app is truly RESTful, it will understand the REST actions and only let you perform certain actions based on the method you use.
或者...您绝对可以使用 HTML 表单。如果应用程序是真正的 RESTful,它会理解 REST 操作,并且只让您根据您使用的方法执行某些操作。
回答by Ronnie Overby
I found this really cool piece of free software called RESTClient.
我发现这个名为RESTClient的免费软件非常酷。
It lets you interact with HTTP resources using various verbs, manually setting headers and the body, setting authentication info, ssl, running test scripts, etc.
它允许您使用各种动词与 HTTP 资源进行交互,手动设置标头和正文,设置身份验证信息、ssl、运行测试脚本等。
This will help me to figure out how to interact with our "webservices" software which is really just a RESTful API to the software's database.
这将帮助我弄清楚如何与我们的“网络服务”软件交互,它实际上只是软件数据库的 RESTful API。
回答by Brian Agnew
You can't PUT using an HTML form (the spec defines only GET/POSTfor forms).
您不能使用 HTML 表单进行 PUT(规范仅定义了表单的GET/POST)。
However any HTTP API should allow you to PUT, in the same way that it allows you to GET or POST. For example, here's the Java HTTPClient documentation, which details PUT alongside all the other HTTP verbs.
然而,任何 HTTP API 都应该允许你 PUT,就像它允许你 GET 或 POST 一样。例如,这里是 Java HTTPClient 文档,其中详细介绍了 PUT 以及所有其他 HTTP 动词。
I don't know which language you're using, but I think it's going to be pretty trivial to write an app to perform an HTTP PUT.
我不知道您使用的是哪种语言,但我认为编写一个应用程序来执行 HTTP PUT 非常简单。
回答by everconfusedGuy
Test the api as a chrome extension https://chrome.google.com/webstore/detail/fdmmgilgnpjigdojojpjoooidkmcomcm
将 api 测试为 chrome 扩展 https://chrome.google.com/webstore/detail/fdmmgilgnpjigdojojpjoooidkmcomcm
回答by user141682
How about giving libcurl.NET a try: http://sourceforge.net/projects/libcurl-net/
试试 libcurl.NET 怎么样:http: //sourceforge.net/projects/libcurl-net/
回答by S.Lott
"Now, the administrator of this system has come to me, wanting to import data into the system by using the webservices component."
“现在,这个系统的管理员来找我了,想用webservices组件把数据导入系统。”
Web services have little to do with HTML forms.
Web 服务与 HTML 表单关系不大。
Web services requests are either done from Javascript (e.g., as Ajax) or they're done from your application programs.
Web 服务请求要么通过 Javascript(例如,作为 Ajax)完成,要么通过您的应用程序完成。
You would write a C# or VB program that used HTTP to do a Put to the given web services URL with the given set of data.
您将编写一个 C# 或 VB 程序,使用 HTTP 使用给定的数据集对给定的 Web 服务 URL 执行 Put。
Here, for instance, is some sample VB code: http://developer.yahoo.com/dotnet/howto-rest_vb.html#post
例如,这里是一些示例 VB 代码:http: //developer.yahoo.com/dotnet/howto-rest_vb.html#post
Replace the method string of "POST" with "PUT".
将“POST”的方法字符串替换为“PUT”。
回答by Srikar Doddi
Just a headsup some network admins block puts for various reasons. So you may have to use a POST instead of PUT. Check with your operations.
只是提醒一下,一些网络管理员出于各种原因阻止了放置。因此,您可能必须使用 POST 而不是 PUT。检查您的操作。

