Change the Uri of a instance of HttpWebRequest? - c#

I have an instance of an HttpWebRequest that I'm intercepting in an event.
I would like to edit the url before the request is sent but I can't find a way of doing this.
The property RequestUri is read only.
I've thought of a few ways but can't seem to find a working solution:
-Using reflection to set the value ?
-Creating a new request and then cloning all properties. not sure how to do that.

If you think in terms of the HTTP protocol, each request is stateless / unique. The only way to link one request to another is programmatically through something like Cookies, but to the HTTP protocol itself the request is unique.
I think the HttpWebRequest object was designed with this in mind. Each HttpWebRequest represents one unique call to a URL and you build up the parameters for that call. If you want to do another request to a different URL you would create a new HttpWebRequest and pass it the state information you are using, ie: Cookie container, header information etc.
The long winded answer to this is the object is designed to have a read only url and the only way to handle it is to:
Use a little reflection hack, such as you have already done, if you absolutely need to use the given HttpWebRequest object you have.
Create a new HttpWebRequest (WebRequest.Create()) and copy your state information over to the new request.

You can use RewritePath to do this.
F.e.
HttpContext.Current.RewritePath("newurl.aspx");

Related

Get HttpRequest full request string

I'm working on a proxy that will relay connections to another server. I had implemented a IHttpHandler that takes requests for a specific address and sends them to my proxy.
My proxy basically starts a sockect connection to the proxied server and reads the original request:
var requestString = new StreamReader(httpRequest.InputStream).ReadToEnd();
My problem arises at this point: the input stream only contains the stream of the body of the HTTP request, not the complete request.
How can I retrieve the full HTTP Request without having to reconstruct it from the HttpRequest object?
I don't know of a definitive one-liner in ASP .NET, but you can get pretty close, as shown in this forum post. What is suggested there is to build the first line out of HttpRequest properties, then add ServerVariables["ALL_RAW"], then the request body in the InputStream as you're already doing.
The only way to get the 'full HTTP Request' object is to actually reconstruct it in some way. I'm not sure you need the whole object for this process though. Perhaps you could just grab a subset of the items in HttpRequest for processing? You could manually concatenate the various portions of the object you need into something new. Once you deviate from using HttpRequest though, it's all custom to me.

HttpRequest vs HttpWebRequest

I have a web page that intercepts POST requests, pulls the username out of the request, and is supposed to forward the request on depending on the username. Now, I notice that the incoming HttpRequest has a Params property, and HttpWebRequest does not. Why is this? Is there a way I can capture the Params data in my outgoing HttpWebRequest?
Thanks.
They're simply two different .Net classes in two different packages:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.71%29.aspx
System.Object
System.MarshalByRefObject
System.Net.WebRequest
System.Net.HttpWebRequest
http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx
System.Object
System.Web.HttpRequest
HttpWebRequest is an old .Net 1.1 thing - I would definitely use Web.HttpRequest if you're planning on refactoring any code (or writing any new code!)
IMHO...
For starters, they're completely different classes, in different namespaces. That being said,
Params is a wrapper that exposes both querystring parameters as well as POST data. When you are construcing a request you can't write to params, you have to specify what kind of data you are actually adding. So you should add it to the target url as a querystring or you can add it to the body of the request and make it a POST.

Easy alternative to HttpWebRequest for POST requests?

I need to trigger an action on a remote server using an http POST request. The server sends a response, either Y or N, to inform me if the action suceeded or not.
I am looking at using HttpWebRequest to do this, but this seems too complex. To use this class you have to set all the headers, such as content type and content length.
Is there a quicker way to send a POST request that doesn't require setting lower level properties such as this?
Try this WebClient
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
byte[] responseArray = myWebClient.UploadData("YOUR URI","POST","DATA to be Posted");
You can try with WebClient class. It's much more simpler and it's basically a wrapper for HttpWebRequest. It encapsulate all this complex logic you're trying to avoid.
I think the easiest built in class in the framework is WebClient. Scott Hanselman has an example on how to perform gets and posts using it. This SO answer also has a good overview on how to post data.
If you have control over the server you're posting to, you might want to consider making it respond with HTTP status codes instead of some custom method.
the wcf web api project has an http client. It's super easy to use. http://wcf.codeplex.com/

Quick newbie question: where to start creating an HTTP request?

I have a regular C# (console) app,and I want to send a HTTP request to a specified URL and get back a response. I would have guessed that the object to use would be HttpRequest, but I can't find a suitable constructor for that object, and MSDN documentation all assumes you've already got a HttpContext from which to get your Request. In my case I don't - and HttpContext also doesn't have a constructor that is intuitively usable - so what is the correct way of doing this?
Try this http://blogs.x2line.com/al/archive/2008/08/29/3544.aspx
may be this is what you are looking for.
Aren't you looking for HttpWebRequest? Note that it's constructor is obsolete:
Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an HttpWebRequest object.

HttpWebRequest build a custom request?

Basically I have HttpListener to get the request from the client. Now I want to send that request with HttpWebRequest. Is there an easy way to take my raw HTTP request from HttpListener and set it all or will I have to make a parser of some sort to set it bit by bit? Thanks
You have to map them yourself, which isn't that much work.
Initially my thought was to use something like AutoMapper to map them, but the properties of the two classes are too different to map them automatically.

Categories

Resources