简体   繁体   English

我如何使用JSON和C#正确发布到php Web服务

[英]How do I properly post to a php web service using JSON and C#

I'm trying to contact a simple login web service that will determine whether the JSON request was successful or not. 我正在尝试联系一个简单的登录Web服务,该服务将确定JSON请求是否成功。 Currently, in the C# program I am geting an error saying a JSON argument is missing. 当前,在C#程序中,我收到一条错误消息,说缺少JSON参数。 The proper URL to request in the web browser is: 在Web浏览器中请求的正确URL是:

https://devcloud.fulgentcorp.com/bifrost/ws.php?json=[{"action":"login"},{"login":"demouser"},{"password":"xxxx"},{"checksum":"xxxx"}]

The code that I have implemented right now in C# is: 我现在在C#中实现的代码是:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace request
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://devcloud.fulgentcorp.com/bifrost/ws.php?");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {

                string json = new JavaScriptSerializer().Serialize(new
                {
                    action = "login",
                    login = "demouser",
                    password = "xxxx",
                    checksum = "xxxx"
                });
                Console.WriteLine ("\n\n"+json+"\n\n"); 

                streamWriter.Write(json);
            }
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Console.WriteLine (result); 
            } 


        }
    }
}

It looks like the sample URL is passing the JSON as a query string - it is a simple GET request. 看起来示例URL正在将JSON作为查询字符串传递-这是一个简单的GET请求。

You are trying to POST the JSON - which is a much better approach that passing JSON in the query string - ie because of length limitations, and the need to escape simple characters such as space. 您正在尝试发布JSON(这是一种比在查询字符串中传递JSON更好的方法),即由于长度限制以及需要转义简单字符(例如空格)的原因。 But it will not work as does your sample URL. 但这不会像您的示例URL那样起作用。

If you are able to modify the server I would suggest modifying the PHP to use $_REQUEST['action'] to consume the data, and the following C# code: 如果您能够修改服务器,我建议您修改PHP以使用$ _REQUEST ['action']来使用数据,以及以下C#代码:

Public static void Main (string[] args)
{
    using (var client = new WebClient())
    {
            var Parameters = new NameValueCollection {
            {action = "login"},
            {login = "demouser"},
            {password = "xxxx"},
            {checksum = "xxxx"}

            httpResponse = client.UploadValues( "https://devcloud.fulgentcorp.com/bifrost/ws.php", Parameters);
            Console.WriteLine (httpResponse);
    }

}

If you must pass JSON as a query string, you can use UriBuilder to safely create the full URL + Query String and then make the GET request - no need to POST. 如果必须将JSON作为查询字符串传递,则可以使用UriBuilder安全地创建完整的URL +查询字符串,然后发出GET请求-无需POST。

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

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