简体   繁体   English

将其转换为C#中的HTTP POST

[英]Translating this to a HTTP POST in C#

I am currently experimenting with the HTTP request. 我目前正在尝试HTTP请求。 I have successfully managed to do get requests and I have read on doing post request with HTTP request. 我已经成功地完成了get请求的工作,并且已经阅读了使用HTTP请求进行后请求的内容。 Now I am trying to work with the yahoo API and in order to use the Yahoo api it states that at 现在我试图与雅虎API工作,以使用雅虎API它指出

The Message Management API can be used to send a message to another Yahoo! 消息管理API可用于将消息发送到另一个Yahoo!。 Messenger contact. 信使联系人。 The API is very simple to use, as shown here. 该API非常易于使用,如下所示。 Note that the contact that the message is sent to is part of the URI, using the following format: <server>/v1/message/<network>/<contactID> 请注意,发送消息的联系人是URI的一部分,使用以下格式: <server>/v1/message/<network>/<contactID>

POST /v1/message/yahoo/targetYahooId?sid=msgrsessionid  
Host: rcore1.messenger.yahooapis.com  
Authorization: < Standard OAuth credentials >  
Content-Type: application/json;charset=utf-8  
Content-Length: 25  
{  
    "message" : "Hey there"  
}  

Now I have an OAuth string which I obtained from get using the HttpWebRequest object. 现在,我有了一个使用HttpWebRequest对象从get获取的OAuth字符串。 The string is something like this 字符串是这样的

oauth_token=A%3Dvh....aRg--&oauth_token_secret=bd46a....c9239&oauth_expires_in=3600&oauth_session_handle=ALtT.....3J1N4Zg--&oauth_authorization_expires_in=784964948&xoauth_yahoo_guid=TUSKED5...NCIA

UPDATE 更新

Now my question are as follows : 现在我的问题如下:

1- If I am using WebRequest object in C# what would my URI look like 1-如果我在C#中使用WebRequest对象,我的URI会是什么样子

2- I understand that it requires a JSON type object. 2-我知道它需要一个JSON类型的对象。 How do i even know what OAuth parameters are ? 我什至不知道什么是OAuth参数?

One thing you'll need to change is the content type: 您需要更改的一件事是内容类型:

request.ContentType = "application/json;charset=utf-8";

And of course, the url. 当然是网址。

you need to change the url on the line with the url in it 您需要更改其中包含网址的行上的网址

you need to change the content-type line 您需要更改内容类型行

you need to make the payload into a json string then convert it to a byte array (byteArray in the sample) 您需要将有效负载转换为json字符串,然后将其转换为字节数组(示例中为byteArray)

either assemble the json by hand "{ foo:'bar'}" etc or use json.net 手动组装JSON(“ {foo:'bar'}”等)或使用json.net

and set the content-length 并设置内容长度

Looks like it's expecting a JSON object for the request body. 看起来它期望请求主体为JSON对象。 Depending on the version of .NET you're using, you can either use a Javascript serializer as shown here ( https://stackoverflow.com/a/7003815/939080 ) or JSON.NET ( http://james.newtonking.com/projects/json-net.aspx ) to convert your form collection into JSON output. 根据您使用的.NET版本,您可以使用此处显示的Javascript序列化器( https://stackoverflow.com/a/7003815/939080 )或JSON.NET( http://james.newtonking)。 com / projects / json-net.aspx )将表单集合转换为JSON输出。

You are asking an open-ended question that would require people to write a bunch of code for you if you want a specific and complete answer. 您在问一个开放式问题,如果您想要一个特定而完整的答案,那将需要人们为您编写一堆代码。 As others have pointed out, there are several issues that you'd need to deal with: 正如其他人指出的那样,您需要解决几个问题:

  • The JSON payload, which would be a straightforward matter of putting the JSON string in the request body via the byteArray used in the code sample. JSON有效负载,这很简单,可以通过代码示例中使用的byteArray将JSON字符串放入请求正文中。
  • The content type, which you would need to change as described by jrummell. 内容类型,您需要按照jrummell的说明进行更改。
  • The OAuth credentials, which is a kettle of fish you'll need to read about, understand, and acquire a library for. OAuth凭证,这是您需要阅读,了解和获取的库的​​宝藏。 Here's a good place to start looking for a library. 这是开始寻找图书馆的好地方

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

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