你怎么做HTTP Put? [英] How do you do an HTTP Put?
问题描述
我们这个软件有一个webservices组件。
We have this software that has a webservices component.
现在,这个系统的管理员来找我,希望通过使用以下方法将数据导入系统webservices组件。
Now, the administrator of this system has come to me, wanting to import data into the system by using the webservices component.
所以,我去阅读文档试图解决这个问题,我看到这样的事情:
So, I went to read the documentation to try to figure this thing out and I am seeing things like this:
点击此处查看我的内容我在谈论(这在firefox,chrome和safari中看起来最好)
该文档提供了相互作用的示例系统使用HTTP动词,如GET,POST,PUT,DELETE。但在我有限的经验中,我从未发送过HTTP PUT或DELETE。
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.
你是怎么做到的?我已经构建了具有method =post或method =get的HTML表单,并且请求被发送到action属性中指定的任何内容(action =someResource)。但我真的不知道如何处理这个PUT事情。
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.
如果我不得不猜测,我将不得不构建一个创建某种HTTP的应用程序请求对象并设置它的所有属性,并以某种方式包含我想要PUT到RESOURCE的数据( )。然后我会用我的编程语言发送请求,等等等等。我只是在猜测这一点。请提供一些帮助!
我正在尝试使用REST术语,这对我来说是一个非常新的东西
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 (). Then I would send the request using my programming language and blah blah blah. I am just speculating on this. Please offer up some assistance!
I am trying to use REST terminology, which is something else is very new to me
我认为我是一名网络开发人员,因为我知道像XHTML,CSS,JavaScript等等,但它开始看起来像我根本不了解网络的基础(HTTP)。
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).
PS:我主要用.net编程。因此,.net中的任何示例都非常棒。
PS: I program mostly with .net. So, any examples in .net would be pretty awesome.
推荐答案
这是使用HttpWebRequest的C#示例:
Here's a C# example using HttpWebRequest:
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);
}
}
更新:现在有了System.Net.Http中的HttpClient类(作为NuGet包提供)这使得这更容易:
Update: there's now an HttpClient class in System.Net.Http (available as a NuGet package) that makes this a bit easier:
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);
}
}
这篇关于你怎么做HTTP Put?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!