How do I create an API server for a mock to send and receive Json?

Asked 2 years ago, Updated 2 years ago, 96 views

I am creating a game with Unity's C#.

using the JsonUtility implemented in Unity I would like to test the continuity of Json.

I don't know how to build a mock API server. I'm looking around for a way to build it.
(I want to be able to do simple tests without bothering server engineers)

■What you want to do
 Serialize class and request to server using HttpWebRequest
 ↓
 Ask the response to return what you submitted in the request as it is, and de-serialize it.

■Environment
 Unity 5.3.0f4
 Windows 7 64-bit

c# json

2022-09-30 17:44

1 Answers

This is an example of implementing a server that uses HttpListener and DataContractJsonSerializer to return JSON for testing.Please add System.Net System.Xml System.Runtime.Serialization to the reference settings.Administrator privileges are required for execution.

using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

class program
{
    static void Main (string[]args)
    {
        using(varh=new HttpListener())
        {
            // TODO—Specifies the hostname and port to listen to.
            h.Prefixes.Add("http://*:58080/";
            h.Start();

            for ( ; ; )
            {
                varc=h.GetContext();

                var request = c. Request;

                // TODO: Now I'm going to separate the URLs.
                Console.WriteLine(req.RawUrl);

                if(req. HasEntityBody)
                {
                    // TODO—This is where the request body is processed.
                    using(varsr=new StreamReader(req.InputStream, new UTF8 Encoding(false)))))
                    {
                        Console.WriteLine(sr.ReadToEnd());
                    }
                }

                // Value you want to return as JSON
                value = new Result() {Value = true, Message = "Aye abcde";

                // Write JSON to the response.
                HttpListenerResponseres=null;
                try
                {
                    res=c.Response;
                    res.ContentType="application/json";
                    newDataContractJsonSerializer(typeof(Result)) .WriteObject(res.OutputStream,value);
                }
                finally
                {
                    res.Close();

                }
            }
        }
    }
}
public class Result
{
    public pool value {get;set;}

    public string message {get;set;}
}

The above code doesn't seem to work with Unity, but if you use the C# compiler that comes standard with Windows 7 even if you don't have Visual Studio,

c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe/out: C:\test\mock.exe/reference:system.net.dll/reference:system.xml.dll/reference:system.serviceemodel.web.dll c:\test\mockhttplistener.cs

You should be able to compile from the command prompt as shown in .After compiling and viewing the app, visit http://localhost:58080/ and you should see JSON in your browser.


2022-09-30 17:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.