Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从RESTWeb服务返回自定义值_C#_Web Services - Fatal编程技术网

C# 如何从RESTWeb服务返回自定义值

C# 如何从RESTWeb服务返回自定义值,c#,web-services,C#,Web Services,我在项目中使用REST web服务,它的返回值是json格式的,如下所示: private void btnCheckStatus_Click(object sender, EventArgs e) { Uri address = new Uri("http://www.asanak.ir/webservice/v1rest/msgstatus"); // Create the web request HttpWebRequest

我在项目中使用REST web服务,它的返回值是json格式的,如下所示:

    private void btnCheckStatus_Click(object sender, EventArgs e)
    {
        Uri address = new Uri("http://www.asanak.ir/webservice/v1rest/msgstatus");

        // Create the web request
        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

        // Set type to POST
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        // Create the data we want to send
        StringBuilder data = new StringBuilder();
        data.Append("username=" + textUserName.Text.Trim());
        data.Append("&password=" + textPassword.Text.Trim());
        data.Append("&msgid=" + textMsgId.Text.Trim());

        // Create a byte array of the data we want to send
        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

        // Set the content length in the request headers
        request.ContentLength = byteData.Length;

        // Write data
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
        }

        // Get response
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            textResult.Text = reader.ReadToEnd();
        }
    }
[{“MsgID”:“92817137”,“状态”:“0”,“发送时间”:“2014-06-11 14:17:40”,“发送时间”:“0000-00-00:00:00”}]

但我不需要所有这些,我只需要“雕像”标签。我该怎么做

我的代码如下:

    private void btnCheckStatus_Click(object sender, EventArgs e)
    {
        Uri address = new Uri("http://www.asanak.ir/webservice/v1rest/msgstatus");

        // Create the web request
        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

        // Set type to POST
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        // Create the data we want to send
        StringBuilder data = new StringBuilder();
        data.Append("username=" + textUserName.Text.Trim());
        data.Append("&password=" + textPassword.Text.Trim());
        data.Append("&msgid=" + textMsgId.Text.Trim());

        // Create a byte array of the data we want to send
        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

        // Set the content length in the request headers
        request.ContentLength = byteData.Length;

        // Write data
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
        }

        // Get response
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            textResult.Text = reader.ReadToEnd();
        }
    }
我希望返回值如下:
“状态”:“0”


谢谢

如果您无法更改Web服务的方法,使其只返回您需要的值,那么唯一的方法就是在代码中自定义响应

// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    StreamReader reader = new StreamReader(response.GetResponseStream());

    //add your code here to customize the return value
    var array = Newtonsoft.Json.Linq.JArray.Parse(reader.ReadToEnd());
    var jObject = new Newtonsoft.Json.Linq.JObject();
    jObject.Add("Status", array[0].Value<string>("Status"));

    textResult.Text = jObject.ToString();

}
//获取响应
使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse)
{
StreamReader=新的StreamReader(response.GetResponseStream());
//在此处添加代码以自定义返回值
var array=Newtonsoft.Json.Linq.JArray.Parse(reader.ReadToEnd());
var jObject=newnewtonsoft.Json.Linq.jObject();
添加(“状态”,数组[0]。值(“状态”);
textResult.Text=jObject.ToString();
}

返回值是什么意思?此方法(btnCheckStatus_Click)不返回任何内容-是否要从web服务读取状态值?@Axarydax是的,我只想从web服务读取状态值。。。但是通过上面的代码,我读了所有这些。。。