0

I am running a server with a mysql database on it. I am now making a C# program which has to put some data into the database. For security reasons I would send the data to a php script which inserts the data in the local mysql database.

I am trying the code below, but when I use fiddler to check if the url is called, it doesn't show up, so it seems as if the url is never called.

My code looks like this:

    string result = string.Empty;
    string data2 = string.Empty;
    string[] postdata = new string[8];
            postdata[0] = "Date";
            postdata[1] = log.EndTime.ToString();
            postdata[2] = "Name";
            postdata[3] = log.OwnerTask.Schedule.Name;
            postdata[4] = "Status";
            postdata[5] = log.ParsedStatus;
            postdata[6] = "Message";
            postdata[7] = log.ParsedMessage;
            string Url = "http://x.x.x.x/send.php";
            System.Text.ASCIIEncoding ascii = new ASCIIEncoding();

            for (int i = 0; i < postdata.Length; i += 2)
            {
                data2 += string.Format("&{0}={1}", postdata[i], postdata[i + 1]);
            }

            data2 = data2.Remove(0, 1);

            byte[] bytesarr = ascii.GetBytes(data2);

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = bytesarr.Length;

                System.IO.Stream streamwriter = request.GetRequestStream();
                streamwriter.Write(bytesarr, 0, bytesarr.Length);
                streamwriter.Close();
                }               

Can somebody help me out? Point me in the right direction?

Thanks

2
  • 5
    For security reasons I would send the data to a php script which inserts the data in the local mysql database. - Seems like the worst possible approach from a security standpoint. PHP stuff can be viewed by anyone with a browser just by pressing "view source". Create a proper Web Service instead. Commented Jun 24, 2014 at 21:53
  • 1
    If you really want to go this route, I'd use the WebClient class which is a higher level wrapper for the HttpWebRequest. It has a nice .UploadString() method if I remember correctly, which does POST requests. Commented Jun 24, 2014 at 21:56

1 Answer 1

1

More focused answer, based on the spot-on comment provided

After writing to the stream, you should invoke GetResponseStream to actually perform the request:

    System.IO.Stream streamwriter = request.GetRequestStream();                
    streamwriter.Write(bytesarr, 0, bytesarr.Length);
    streamwriter.Close();
    var response = request.GetResponseStream(); // this will execute the request
    // [go on ...]
}  

Old answer, spotted the issue but missed the purpose of OP code, left for reference

I'm not able at the moment to verify it in VS but this

System.IO.Stream streamwriter = request.GetRequestStream();
                                           ^^^^^^^

should be

System.IO.Stream streamwriter = request.GetResponseStream();
                                           ^^^^^^^^

as in: you want to parse the response, not the request (which your code never executes, that's why you don't see the URL being hit).

Sign up to request clarification or add additional context in comments.

2 Comments

He needs both. First write the bytes to the request stream, then get the response from the response stream.
You're right, I missed that. I expanded the answer to be more focused on the specific need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.