tags:

views:

1164

answers:

12

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?

Many thanks

Andrea

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.

A: 

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.

Lasse V. Karlsen
A: 

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.

Joel Coehoorn
A: 

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(''))
Hank Gay
A: 

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...

chakrit
+1  A: 

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

JasonS
+1  A: 

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.

sectrean
+3  A: 

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);
Ryan Duffield
A: 

@Ryan Duffield - WebClient

I tried, adding also

client.Headers.Add(data);

but I get a "(400) Bad Request" on the client.UploadValues line.

@sectrean - WebRequest

The same problem: when I do

response = request.GetResponse();

I get the same error.

Now tomorrow (it's 23:30 here in Italy) I'll get back in touch with my "counterpart" in order to have a feedback on what they received during my (many) calls.

Anyway - Really Thanks a lot to everybody, I'll post here the final solution as soon as I find it :-)

ila
A: 

Hi, 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...

kudlur
A: 

@kudlur - thanks for your sample, I tried it in the "final" environment, tomorrow morning I'll know how it worked; btw I could not use it in debug in VStudio, when the "client" page calls the CheckHeader the breakpoint is not fired, don't know why.

ila
A: 

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 :-)

ila
A: 

@kudlur - your response seems to be almost exactly what I am looking for in one of my projects. I just have one more question.

If I wanted to display the results of Request.Headers[key].ToString(); or pass it to another page to do something, how do I go about doing this?

I tried Response.Redirect("accepter.aspx?param=" + Request.Headers[key].ToString()), and the code in the page_load of all of these forms actually runs, but the original html page with the javascript is still the only page that actually shows in the browser.

Thanks for any help you can offer.

I'm very excited about StackOverflow...so far it seems that all of the intelligent people that haven't been commenting at YouTube are hanging out here instead. :)