Question

I have to POST some parameters to a URL outside my network, and the developers on the other side asked me to not use HTTP Parameters: instead I have to post my key-values in HTTP Headers.

The fact is that I don't really understand what they mean: I tried to use a ajax-like post, with XmlHttp objects, and also I tried to write in the header with something like

Request.Headers.Add(key,value);

but I cannot (exception from the framework); I tried the other way around, using the Response object like

Response.AppendHeader("key", "value");

and then redirect to the page... but this doesn't work, as well.

It's evident, I think, that I'm stuck there, any help?


EDIT I forgot to tell you that my environment is .Net 2.0, c#, on Win server 2003. The exception I got is

System.PlatformNotSupportedException was unhandled by user code
  Message="Operation is not supported on this platform."
  Source="System.Web"

This looks like it's caused by my tentative to Request.Add, MS an year ago published some security fixes that don't permit this.

Was it helpful?

Solution

Like @lassevk said, a redirect won't work.

You should use the WebRequest class to do an HTTP POST from your page or application. There's an example here.

OTHER TIPS

Have you tried the WebClient class? An example might look like:

        WebClient client = new WebClient();
        NameValueCollection data = new NameValueCollection();
        data["var1"] = "var1";
        client.UploadValues("http://somewhere.com/api", "POST", data);

Take a look at HttpWebRequest. You should be able to construct a request to the URL in question using HttpWebRequest.Method = "POST".

You should post more information.

For instance, is this C#? It looks like it, but I might be wrong.

Also, you say you get an exception, what is the exception type and message?

In any case, you can't redirect to a page for POST, you need to submit it from the browser, not from the server redirect, so if you want to automate this, I would guess you would need to generate a html page with a form tag, with some hidden input fields, and then submit it with javascript.

I think they mean they don't want you to use URL parameters (GET). If you use http headers, it's not really querying through POST any more.

What language/framework?

Using Python and httplib2, you should be able to do something like:

http = httplib2.Http()
http.request(url, 'POST', headers={'key': 'value'}, body=urllib.urlencode(''))

I believe that the Request object would only accept a certain set of predefined headers.

There's an enumeration that lists all the supported HTTP Headers too.

But I can't remember it at the moment... I'll look it up in a sec...

I tested your scenario using 2 sample pages using XmlHttpRequest option. Custom headers are available in the aspx page posted to, using XmlHttpRequest.

Create the following 2 pages. Make sure the aspx page is in a solution , so that you can run the in the debugger, set break point and inspect the Request.Header collection.

<html>

<head>

&lt; script language="javascript"&gt;

function SendRequest()
{
    var r = new XMLHttpRequest();
    r.open('get', 'http://localhost/TestSite/CheckHeader.aspx');
    r.setRequestHeader('X-Test', 'one');
    r.setRequestHeader('X-Test', 'two');
    r.send(null);

}
&lt; script / &gt;

</head> <body> <form> <input type="button" value="Click Me" OnClick="SendRequest();" /> </form> </body> </html>


CheckHeader.aspx

using System;

using System.Web;

using System.Web.UI;

public partial class CheckHeader : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)
{
    string value = string.Empty;
    foreach (string key in Request.Headers)
        value = Request.Headers[key].ToString();
}

}

Man.. This html editor sucks.. or i do not know how to use it...

The exception I was facing yesterday was caused by my stupid try to write on the headers of the already built page.

When I started creating my Request following one of the mothods indicated here, I could write my headers.

Now I'm using the WebRequest object, as in the sample indicated by @sectrean, here.

Thanks a lot to everybody. StackOverflow rocks :-)

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