在 .net 中设置 post 参数的等效方法是什么?
我必须与第三方 API 集成。要使用该服务,我必须使用某些参数“POST”到特定网址。
该服务提供的示例代码位于 php 中,如下所示
$data = array('From' => '0999999', 'To' => '08888888');
$curl = curl_init();
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); <--- Ignore SSL warnings
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
我尝试使用 WebRequest 类在 .net 中实现相同的目的。但是,我对如何设置后参数数据有点困惑。我认为上面的 $data 只是一个字典。所以我创建了一个等效的字典。但是,如何使用字典值设置 post 参数?
在 http://msdn.microsoft.com/en-us/library/debx8sh9.aspx ,他们将字符串序列化为字节数组,然后将 is 设置为 dataStream 中的 post 参数。我如何对字典做同样的事情?
或者我的方法不正确?有更好的方法吗?
I have to integrate with a third party API. To use the service, I have to "POST" to a specific url with certain parameters.
The example code provided by the service is in php and is as follows
$data = array('From' => '0999999', 'To' => '08888888');
$curl = curl_init();
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); <--- Ignore SSL warnings
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
I am trying to use the WebRequest class to achieve the same in .net. However, I am a bit confused about how to set the post parameter data. I figured $data above is nothing but a Dictionary. So I created a equivalent dictionary. However, how do I set the post parameters with the dictionary values?
In http://msdn.microsoft.com/en-us/library/debx8sh9.aspx, they have serialized a string to a byte array and then set is as the post parameter in the dataStream. How do I do the same for a Dictionary?
Or is my approach incorrect? Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,
WebClient.UploadValues
将是最简单的方法; 请参阅 MSDN 了解完整示例 。但请注意,这仅涵盖CURLOPT_POSTFIELDS
和CURLOPT_POST
。错误失败是自动且隐式的,并且响应已作为byte[]
包含在内。即
注意
POST
在这里是隐式的;如果您需要不同的 http 方法,请使用重载:Generally,
WebClient.UploadValues
is going to be the easiest approach here; see MSDN for a full example. Note, however, that this only coversCURLOPT_POSTFIELDS
andCURLOPT_POST
. Fail on error is automatic and implicit, and the response is already included as abyte[]
.i.e.
note
POST
is implicit here; if you need a different http method, use the overload:如果您使用 url 编码的发布数据,您可以使用 HttpServerUtility.UrlEncode 方法(字符串)
If you are using url encoded post data you can url encode each key/value pair of your dictionary using HttpServerUtility.UrlEncode Method (String)
以防万一您必须使用 HttpWebRequest,下面的代码会将名为
formVars
的Dictionary
转换为byte[ ]
名为toPost
:在 https://dotnetfiddle.net/IOzIE6
Just in case you have to use HttpWebRequest, the code below converts a
Dictionary<String,String>
namedformVars
to abyte[]
namedtoPost
:Play with a working copy at https://dotnetfiddle.net/IOzIE6
您可能想要使用
http://msdn.microsoft.com /en-us/library/system.net.httpwebrequest.aspx
You probably want to use the
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx