使用 C# 进行 json 调用
我正在尝试使用 C# 进行 json 调用。我尝试创建一个呼叫,但没有成功:
public bool SendAnSMSMessage(string message)
{
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://api.pennysms.com/jsonrpc");
request.Method = "POST";
request.ContentType = "application/json";
string json = "{ \"method\": \"send\", "+
" \"params\": [ "+
" \"IPutAGuidHere\", "+
" \"[email protected]\", "+
" \"MyTenDigitNumberWasHere\", "+
" \""+message+"\" " +
" ] "+
"}";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(json);
writer.Close();
return true;
}
任何有关如何进行这项工作的建议将不胜感激。
I am trying to make a json call using C#. I made a stab at creating a call, but it did not work:
public bool SendAnSMSMessage(string message)
{
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://api.pennysms.com/jsonrpc");
request.Method = "POST";
request.ContentType = "application/json";
string json = "{ \"method\": \"send\", "+
" \"params\": [ "+
" \"IPutAGuidHere\", "+
" \"[email protected]\", "+
" \"MyTenDigitNumberWasHere\", "+
" \""+message+"\" " +
" ] "+
"}";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(json);
writer.Close();
return true;
}
Any advice on how to make this work would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的代码中,您没有获得 HttpResponse,因此您看不到服务器端发回的内容。
您需要以类似于获取(发出)请求的方式获取响应。因此,
我还在 pennysms 文档中注意到,他们期望内容类型为“text/json”而不是“application/json”。这可能没有什么区别,但值得一试,以防万一不起作用。
In your code you don't get the HttpResponse, so you won't see what the server side sends you back.
you need to get the Response similar to the way you get (make) the Request. So
I also notice in the pennysms documentation that they expect a content type of "text/json" and not "application/json". That may not make a difference, but it's worth trying in case it doesn't work.
只是继续 @Mulki 用他的代码所做的事情
just continuing what @Mulki made with his code
以下是 Shiv Kumar 的答案的变体,使用 Newtonsoft.Json(又名 Json.NET):
Here's a variation of Shiv Kumar's answer, using Newtonsoft.Json (aka Json.NET):
如果你的函数驻留在 mvc 控制器中,你可以使用下面的代码和你想要转换为 json 的字典对象
也可以尝试查看 system.web.script.serialization.javascriptserializer 如果您使用 .net 3.5
作为您的网络请求......看起来乍一看还不错..
我会使用这样的东西..
也许你可以将 post 和 json 字符串作为参数,并将其用作所有调用的通用 webrequest 方法。
If your function resides in an mvc controller u can use the below code with a dictionary object of what you want to convert to json
Also try and look at system.web.script.serialization.javascriptserializer if you are using .net 3.5
as for your web request...it seems ok at first glance..
I would use something like this..
May be you can make the post and json string a parameter and use this as a generic webrequest method for all calls.
它只是一个如何在 BIDS 2008 中使用 System.Net.WebRequest 发布 Json 数据以及从 Rest API 获取 Json 数据以及不使用 newtonsoft 的示例。这只是一个示例代码,绝对可以进行微调(经过充分测试,它可以正常工作并像魅力一样满足我的测试目的)。它只是给你一个想法。我想要这个帖子,但找不到,因此发布了这个。这些是我拉这个帖子的主要来源。
链接 1 和 链接2
有效的代码(经过单元测试)
BIDS 2008(具有SP1和3.5框架)内我的测试脚本任务中的参考
Its just a sample of how to post Json data and get Json data to/from a Rest API in BIDS 2008 using System.Net.WebRequest and without using newtonsoft. This is just a sample code and definitely can be fine tuned (well tested and it works and serves my test purpose like a charm). Its just to give you an Idea. I wanted this thread but couldn't find hence posting this.These were my major sources from where I pulled this.
Link 1 and Link 2
Code that works(unit tested)
References in my test script task inside BIDS 2008(having SP1 and 3.5 framework)