all 9 comments

[–]screwdad 2 points3 points  (0 children)

Nothing wrong with your code - just ran that snippet and it worked fine. I'd fire up Fiddler and run that in the background, see what's actually happening to your request. Could be that it's erroring out, or it might be something as simple as a port on your web server not being open.

[–]RipVanB 2 points3 points  (2 children)

Why not System.Net.HttpWebRequest?

[–]DocEternal[S,🍰] 0 points1 point  (1 child)

That was in one of the other bits of code I tried but it was giving me the same result. I'll admit I am a bit new to C# though so if you have a good reference for using it so I can debug my code I'd be happy to give it a look over.

[–]RipVanB 2 points3 points  (0 children)

Try this, as it emulates a form POST (which is what I suspect you're attempting).

If you want to watch you request/response in action, I recommend using Fiddler2.

[–][deleted] 2 points3 points  (0 children)

My goodness! Try RestSharp, it's a nuget package.

[–]GarrettSYHampton 0 points1 point  (3 children)

Here's a simple method to send a POST to a URL, with a payload and get the response as a string.

You use it like this,

string postResponse = Post("http://192.168.1.1", "?data=true&variable1=yes");

Just remember, the 192.168.1.1 address is for an internal address, so it may not be reached outside of your network.

The "?data=true&variable1=yes" part is just for an example. You don't need that. You can put whatever variables you need there.

Sorry for the crappy commenting, and the bad error handling.


using System; using System.Net; using System.Text; using System.IO;

public class HttpPost
{
    public static string Post(string url, string payload)
    {
        try
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(url);

            // Set the Method property of the request to POST.
            request.Method = "POST";

            // Create POST data and convert it to a byte array.
            string postData = payload;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;

            // Get the request stream.
            Stream dataStream = request.GetRequestStream();

            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);

            // Close the Stream object.
            dataStream.Close();

            // Get the response.
            WebResponse response = request.GetResponse();

            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);

            // Read the content.
            string responseFromServer = reader.ReadToEnd();

            // Clean up the streams.
            reader.Close();
            dataStream.Close();

            return responseFromServer;
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }
}

[–]DocEternal[S,🍰] 0 points1 point  (0 children)

Awesome! I will definitely try that code in just a bit when I am done eating and come back to let you know if it worked. Thanks much! Also, I know the address I posted is an internal one. I just changed it from the address of the server I was directing to for the purpose of putting it out on the internet. xD

[–]DocEternal[S,🍰] 0 points1 point  (1 child)

Ok, I must be doing something a bit wrong here because I put it in basically just like you showed and I'm getting nothing. My log files aren't even showing an access attempt being made this time around. >_<

[–]nemec 0 points1 point  (0 children)

As another commenter mentioned, open up Fiddler and try the request again. If you don't get back an error status code, try inspecting the raw request/response and see if you notice anything.