0

I want to store some simple user data to a JSON file on my server so I can use it with my PHP. I know how to create a JSON file thanks to this handy article: How to write a Json file in C#? But in this article they save the file locally but I want it to save it on my external server but don't know how since I never really used JSON before. Can someone tell me if it's possible and so how what kind of method do I need to use? I thought something like this would work, atleast it seemed reasonable to me:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + ConfigurationManager.AppSettings["jsonuri"] +"/apptest/saleskickerbuffer.txt");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var sw = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    if(reqCat == "bvg")
    {
        json = "{\"bedrijfsNaam\":\"" + bedrijfsNaam + "\"," +
                        "\"ContPers\":\"" + ContPers + "\"," +
                        "\"TelNum\":\"" + TelNum + "\"," +
                        "\"email\":\"" + email + "\"," +
                        "\"Land\":\"" + Land + "\"," +
                        "\"Plaats\":\"" + Plaats + "\"," +
                        "\"PostCode\":\"" + PostCode + "\"}";
        MessageBox.Show(json);
    }

    sw.Write(json);
    sw.Flush();
    sw.Close();
}

This resulted in a failure ofcourse because it did nothing. This is bacause I have never used JSON before.

Can someone teach me a good method for this if it's posssible?

Community
  • 1
  • 1
Berend Hulshof
  • 1,039
  • 1
  • 16
  • 36
  • 1
    Use FTP instead of HTTP. FTP is a form of HTTP that allows files to be uploaded and downloaded. – jdweng Jun 02 '16 at 13:39
  • 4
    Remove json from the equation, do you know how to store a file on a remote server? – Gusman Jun 02 '16 at 13:39
  • Yes I do know how to store files to a remote server @Gusman – Berend Hulshof Jun 02 '16 at 13:42
  • 1
    I think you wan't to say that I need to combine the linked article with the method to save data to a remote serverr? – Berend Hulshof Jun 02 '16 at 13:43
  • @B.Hulshof then what makes the difference between a file containing a text and a file containing a json? :D yes, if you know how to store a file on the server, just create the file locally and send it, that's all, even better, to send a file you will read first the content and then send the content, and in your code you already have it, you can send it directly. – Gusman Jun 02 '16 at 13:47
  • I'm having a bad day, sorry. This was an easy question XD. But thanks for the tip – Berend Hulshof Jun 02 '16 at 13:49

1 Answers1

1

Sorry for the late answer but I got the right code. Instead of HttpWebRequest I had to use just WebRequest and in I also had to use ftp instead of http in my uri string. Here is my final code:

    static string ftpUser = ConfigurationManager.AppSettings["ftpUser"].ToString();
    static string ftpPass = ConfigurationManager.AppSettings["ftpPass"].ToString();

    static string serverPath = ConfigurationManager.AppSettings["jsonuri"].ToString() + dt.bedrijfsNaam + ".json";

    static string json;

    public static void post(string reqCat)
    {
        WebRequest hwr = WebRequest.Create(serverPath);
        hwr.Method = WebRequestMethods.Ftp.UploadFile;
        hwr.Credentials = new NetworkCredential(ftpUser, ftpPass);

        if (reqCat == "bvg")
        {
            json = "[\"bedrijfsNaam\":\"" + bedrijfsNaam + "\"," +
                            "\"ContPers\":\"" + ContPers + "\"," +
                            "\"TelNum\":\"" + TelNum + "\"," +
                            "\"email\":\"" + email + "\"," +
                            "\"Land\":\"" + Land + "\"," +
                            "\"Plaats\":\"" + Plaats + "\"," +
                            "\"PostCode\":\"" + PostCode + "\"]";
            using (var sw = new StreamWriter(hwr.GetRequestStream()))
            {
                sw.Write(json);
                sw.Flush();
                sw.Close();
            }
        }
    }

Also, thank you gusman for the great tip!

Berend Hulshof
  • 1,039
  • 1
  • 16
  • 36