简体   繁体   English

在C#中使用REST PUT

[英]Using REST PUT in C#

I am working with a REST API and I am trying to do a PUT method to it. 我正在使用REST API,并且正在尝试对其执行PUT方法。 I found this code I was going to give a try: 我找到了下面要尝试的代码:

    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);
    }

One thing I want to do if possible, and can't seem to find anything about it. 如果可能的话,我想做一件事,但似乎找不到任何东西。 I would like to pass the data of text fields so, txtEmail.Text , txtFirstName.Text , etc. Is this possible? 我想传递文本字段的数据, txtEmail.TexttxtFirstName.Text等。这可能吗? If, so how would I go about doing this? 如果可以,那我该怎么做呢? Does this code look like it would work? 这段代码看起来可行吗? Unfortunately the API I'm working with has very very little documentation. 不幸的是,我使用的API的文档很少。 Thanks! 谢谢!

The code lines 代码行

Stream dataStream = request.GetRequestStream();
dataStream.Write(arr, 0, arr.Length);
dataStream.Close();

do write something to the remote website. 一定要写一些东西到远程网站。 The request stream is the way to provide data to the server, so you would create a string / object that you write to the stream to transfer to the server. 请求流是向服务器提供数据的方式,因此您将创建一个字符串/对象,并将其写入流中以传输到服务器。 In your example <xml>...</xml> is sent to the server. 在您的示例中, <xml>...</xml>发送到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM