How to modify Http Method in C#? - c#

In HttpWebRequest a user can pass Method as Post OR Get OR Put OR DELETE etc.
But for our specific project I need to pass HttpMethod as "POST /api/login HTTP/1.0".
But this is not allowed in .NET CF.
Then can any one tell me workaround for this please for customizing Http Method.
Thanks in advance.

The reason that this isn't allowed in .NET CF is because it's nonsensical: there isn't a HttpMethod matching what you listed.
I think you are mistaken. Your method is POST, your target url is /api/login and the protocol is HTTP/1.0. So, you should be making a HttpWebRequest to that Url.

Like the manual states:
The Method property can be set to any of the HTTP 1.1 protocol verbs: GET, HEAD, POST, PUT, DELETE, TRACE, or OPTIONS.
So simply use hbWebRequest.Method = "POST";.

Related

ASP.NET HTTP PUT and DELETE produce 404 status

I am trying to get familiar with ASP.NET, but I'm stuck at this basic thing.
So this is my HomeController.
I am using postman to simulate HTTP requests, but only GET method is working. PUT and DELETE both produce 404.
I have tried changing all the annotations to HttpGet, just to make sure that I'm targeting a correct method in my URL and it produces correct results. When I change the annotations back to HttpPut and HttpDelete, with the same links (but changed methods accordingly in postman) I get 404 again...
What am I doing wrong?
seems you are calling bad just need make the request without the method like this
http://localhost:5237/Home
don't use "/Put" same rule for "Delete" or "Post"
if your are using jQuery ajax
$.ajax({
url: 'http://localhost:5237/Home',
type: 'PUT'<---GET, POST, PUT, DELETE
....
})
I reproduced the scenario and tested through POSTMAN its working for me.
Please select the verb as in figure
Seems the url to be used as below where xxxx is port number:
PUT = (http://localhost:xxxx/home/put)
DELETE = (http://localhost:xxxx/home/delete)
#Nemanja, Please let me know if this works.

Add custom header on OPTIONS request only

I want to enable CORS for a subdomain only, so I need to enable it programmatically, I thought doing this would be enough:
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
But then I noticed that the JS library making the requests, makes an OPTIONS request first, so my code never runs.
Is there anyway to show that I have Access-Control-Allow-Origin in an OPTIONS request, but then allow or disallow it programmatically in a GET request?
Thank you
You can easily do so by writing a module, which is similar to what I described in this blog post,
https://blog.lextudio.com/2014/11/how-to-handle-cors-preflight-requests-in-asp-net-mvcweb-api-with-windows-authentication/

The requested resource does not support http method 'PUT'

I have implemented Web API Controller with PUT method (I marked method with HttpPut attribute from System.Web.Http).
And when I try to make put request, i have message
The requested resource does not support http method 'PUT'.
I remove webdav in Web.Config, added put etc.
But it is still not work.
How I can resolve this problem?
Make sure that the param names on your URL match with some param names in your controller method declaration. This simple mistake can cause 405.

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.

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.

Categories

Resources