简体   繁体   中英

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. Currently, in the C# program I am geting an error saying a JSON argument is missing. The proper URL to request in the web browser is:

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:

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.

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. But it will not work as does your sample 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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