Question

I'm trying to make a simple HTTP post a endpoint with ONLY url arguments.

At least thats how I understand the following instructions:

POST to that address with a single parameter named url, the address of the feed that changed.

As with the XML-RPC method, it verifies that the feed has changed, and if so it notifies the subscribers.

The event is logged. The return value is an XML message named result, with two attributes, success and msg.

This is my code currently:

        public static void ping(string feed)
    {
        HttpWebResponse response = MakeRequest(feed);
        XmlDocument document = new XmlDocument();

        document.Load(response.GetResponseStream();
        string success = document.GetElementById("success").InnerText;
        string msg = document.GetElementById("msg").InnerText;

        MessageBox.Show(msg, success);
    }
        private static HttpWebResponse MakeRequest( string postArgument)
    {
        string url = path + "?" + UrlEncode(postArgument);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        return (HttpWebResponse)request.GetResponse();
    }
    private static string UrlEncode( string value)
    {
        string result;
        result= HttpUtility.UrlEncode("url") + "=" + HttpUtility.UrlEncode(value);
        return result;
    }

I'm getting an incorrect response from the server so i assume I'm doing it wrong somehow. here is the response:

Invalid at the top level of the document. Error processing resource 'file:///C:/Users/ADMIN/AppData/Local/Temp/VSD1.tmp.XML...

rue ^

Any ideas??

Thanks in advance

Was it helpful?

Solution 3

Here is the code that I've found to work.

We use a HTTP POST, encoding parameters in the body of the stream. This gives us "url-foobar" as the content body. No content types,dispositions, boundaries, etc in the body.

        private static HttpWebResponse MakeRequest(string path, string postArgument)
    {
        //string url = path + "?" + UrlEncode(postArgument);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

        request.Method = "POST";
        string boundary = Guid.NewGuid().ToString().Replace("-", "");
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        Stream stream = request.GetRequestStream();
        string result = string.Format("url={0}", postArgument);
        byte[] value = Encoding.UTF8.GetBytes(result);
        stream.Write(value, 0, value.Length);
        stream.Close();

        return (HttpWebResponse)request.GetResponse();
    }
        public static void ping(string server, string feed)
    {
        HttpWebResponse response = MakeRequest(server, feed);
        XmlDocument document = new XmlDocument();
        string result = GetString(response.GetResponseStream());
        try
        {
            document.LoadXml(result);
        }
        catch
        {
            MessageBox.Show("There was an error with the response", "Error");
        }
        //MessageBox.Show(msg, success);

    }
    public static string GetString(Stream thestream)
    {
        int n = thestream.ReadByte();
        byte[] bytes = new byte[n];
        thestream.Read(bytes, 0, n);
        return Encoding.ASCII.GetString(bytes);
    }

The calls to GetString are for debugging purposes only and are not strictly needed.

Thanks for everyone who chimed in here for putting me on the right track.

OTHER TIPS

I don't know the API of .NET, but: I understand the instructions as: "execute a post on the URL with the query arguments", and the post body should have the url=foobar parameter.

IOW: instead of taking the postArgument and append it to the url, you're supposed to call the url and provide the properly encoded url=foobar in the message body.

Additionally: I don't see you setting the request 'Accept' header, which may be important if the server uses that to recognize what format to respond with.

The best way to do a forms post in .NET is to use the WebClient class. That class will do the correct thing to send the data, as either a POST (with parameters in entity body) or as a GET (with parameters encoded as query string arguments).

And you need to show us the actual exception that the client is giving back, in order to figure out what is wrong..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top