你怎么做一个HTTP Put? [英] How do you do an HTTP Put?

查看:26
本文介绍了你怎么做一个HTTP Put?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有这个带有网络服务组件的软件.

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" 的 HTML 表单.或方法=获取"并且请求被发送到动作属性中指定的任何内容(动作=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 请求对象并设置它的所有属性,并以某种方式包含我想放入 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 (


I am trying to use REST terminology, which is something else is very new to me
). 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!

我以为我是一名 Web 开发人员,因为我知道 XHTML、CSS、JavaScript 等内容.但现在看来我对 Web 的基础 (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆