最佳呼叫从.NET控制台JSON的WebService方式 [英] Best way to call a JSON WebService from a .NET Console

查看:109
本文介绍了最佳呼叫从.NET控制台JSON的WebService方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我托管在ASP.Net MVC3 Web服务,它返回一个JSON字符串。什么是调用从C#控制台应用程序的Web服务,并解析返回到.NET对象的最佳方式?

I am hosting a web service in ASP.Net MVC3 which returns a Json string. What is the best way to call the webservice from a c# console application, and parse the return into a .NET object?

我应该引用MVC3在我的控制台应用程序?

Should I reference MVC3 in my console app?

Json.Net有序列化和反序列化.NET对象一些不错的方法,但我看不出它有张贴,并从web服务歌厅值的方式。

Json.Net has some nice methods for serializing and deserializing .NET objects, but I don't see that it has ways for POSTing and GETing values from a webservice.

或者我应该只是创造张贴和歌厅到Web服务我自己的辅助方法?如何将我的序列化.NET对象,以键值对?

Or should I just create my own helper method for POSTing and GETing to the web service? How would I serialize my .net object to key value pairs?

推荐答案

我使用HttpWebRequest的从Web服务,它返回我一个JSON字符串就搞定了。它看起来是这样的一个GET:

I use HttpWebRequest to GET from the web service, which returns me a JSON string. It looks something like this for a GET:

// Returns JSON string
string GET(string url) 
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    try {
        WebResponse response = request.GetResponse();
        using (Stream responseStream = response.GetResponseStream()) {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            return reader.ReadToEnd();
        }
    }
    catch (WebException ex) {
        WebResponse errorResponse = ex.Response;
        using (Stream responseStream = errorResponse.GetResponseStream())
        {
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            String errorText = reader.ReadToEnd();
            // log errorText
        }
        throw;
    }
}

然后我使用 JSON.Net 动态解析字符串。
或者,您可以使用此codePLEX工具生成样品JSON输出静态的C#类: HTTP://jsonclassgenerator.$c$ cplex.com/

POST看起来是这样的:

POST looks like this:

// POST a JSON string
void POST(string url, string jsonContent) 
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    Byte[] byteArray = encoding.GetBytes(jsonContent);

    request.ContentLength = byteArray.Length;
    request.ContentType = @"application/json";

    using (Stream dataStream = request.GetRequestStream()) {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }
    long length = 0;
    try {
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
            length = response.ContentLength;
        }
    }
    catch (WebException ex) {
        // Log exception and throw as for GET example above
    }
}

我用code这样的在我们的Web服务的自动化测试。

I use code like this in automated tests of our web service.

这篇关于最佳呼叫从.NET控制台JSON的WebService方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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