C#REST客户端使用POST发送数据 [英] C# REST client sending data using POST

查看:128
本文介绍了C#REST客户端使用POST发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个简单的POST请求到一个REST Web服务和打印响应(代码如下,其中大部分来自雅虎开发者文档,并提供了一​​些文档的MSDN代码片段拍摄)。我希望客户端发送:

I'm trying to send a simple POST request to a REST web service and print the response (code is below, mostly taken from Yahoo! developer documentation and the MSDN code snippets provided with some of the documentation). I would expect the client to send:

请求方法:POST(即我希望$ _ SERVER ['REQUEST_METHOD'] == PHP中的POST)结果
数据为:富=栏(即$ _ POST ['富'] ==在PHP'酒吧')

Request Method: POST (i.e. I expect $_SERVER['REQUEST_METHOD'] == 'POST' in PHP)
Data: foo=bar (i.e. $_POST['foo'] == 'bar' in PHP)

不过,这似乎是发送​​:

However, it seems to be sending:

请求方法:FOO = BARPOST结果
数据:(空白)

Request Method: FOO=BARPOST
Data: (blank)

我知道API工作原理我已经用Python写的和PHP客户测试过,所以我敢肯定它必须与我的C#的一个问题。我不是通过贸易的.NET程序员,所以希望任何意见/如何找出问题所在指针 - 我敢肯定这件事情微不足道,但我看不出它自己

I know the API works as I've tested it with clients written in Python and PHP, so I'm pretty sure it must be a problem with my C#. I'm not a .NET programmer by trade so would appreciate any comments/pointers on how to figure out what the problem is - I'm sure it's something trivial but I can't spot it myself.

URI,用户名和密码变量先前在代码中设置 - 他们做工精细用GET请求

uri, user and password variables are set earlier in the code - they work fine with GET requests.

request = (HttpWebRequest) WebRequest.Create(uri);
request.Credentials = new NetworkCredential(user, password);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";

string postData = "foo=bar";
request.ContentLength = postData.Length;

StreamWriter postStream = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
postStream.Write(postData);
postStream.Close();

response = (HttpWebResponse) request.GetResponse();



REST API使用PHP编写,并使用时,$ _POST阵列是服务器上的空。C#的客户端

The REST API is written in PHP, and the $_POST array is empty on the server when using the C# client.

推荐答案

最后发现HttpWebRequest.PreAuthenticate属性,这似乎如果代码编辑,像这样来解决问题:

Eventually found the HttpWebRequest.PreAuthenticate property which seems to solve the problem if the code is edited like so:

request = (HttpWebRequest) WebRequest.Create(uri);
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(user, password);
request.Method = WebRequestMethods.Http.Post;



从文档我相信这股势力验证实际发送POST请求之前。我不知道为什么这个类不会自动执行此操作(其他语言库,使得这一过程透明的,除非你明确地将其关闭),但它已经解决了这个问题对我来说,可能拯救别人搜索的另一个2天而拔头发。

From the documentation I presume this forces authentication before the actual POST request is sent. I'm not sure why the class doesn't do this automatically (libraries for other languages make this process transparent, unless you explicitly turn it off), but it has solved the problem for me and may save someone else another 2 days of searching and hair-pulling.

有关它的价值,预认证并不需要G​​ET请求,只有POST进行设置,但如果你将其设置为一个GET请求一切都将仍然有效,但所需时间稍长。

For what it's worth, PreAuthenticate doesn't need to be set for GET requests, only POST, although if you do set it for a GET request everything will still work, but take slightly longer.

这篇关于C#REST客户端使用POST发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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