开发者

c# app: Is it possible to implement a JSON interface?

I've built a simple C# app (.Net 4.0 + WPF) which can send and receive JSON messages v开发者_运维知识库ia TCP sockets.

As a next step, it should be possible that JavaScript apps on websites and PHP scripts can send and receive JSON messages to/from my app. Is that possible?

Since JS/PHP will use stateless HTTP connections, how should a request to my app work, for example, should the JS/PHP apps send a JSON message to my app and my app response (HTTP response) with a JSON message? Is that even possible? And should I use GET or POST method to send the JSON messages to/from my app?

Hope my questions do not cause too much confusion ;-) I but I appreciate every tip, clarification or feedback you can give me.

Mike


You can accomplish this via a .NET web service using special JSON directives on the web method, e.g.

[ScriptMethod(UseHttpGet = true, ResponseFormat=ResponseFormat.Json)]
public string DoSomething(string param1, int param2) 
{
   // Do Something
}

When the ResponseFormat.Json property is specified, the data returned will be serialized into the appropriate JSON format. Also note, in order to recieve a true JSON response, you'll need to set your content-type to "application/json" from the requesting application. Otherwise, the method will attempt to wrap the response in XML.

Also, I am enabling a HttpGet on this method so that you can post via a query string to the method, e.g.

http://www.example.com/service.asmx?param1='Hello'&param2=1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜