I’m using HttpWebRequest to POST data to an ASP.NET webpage (currently running on the ASP.NET Development Server) Here’s the code;
string url = "http://localhost:3333/MySite/";
WebResponse response = null;
try {
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "data=test";
byte[] dataBtyes = encoding.GetBytes(postData);
request.UserAgent = "Custom Agent";
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = dataBtyes.Length;
Stream stream = request.GetRequestStream();
stream.Write(dataBtyes, 0, dataBtyes.Length);
stream.Close();
response = request.GetResponse();
} catch (Exception e) {
}
Using a HTTPModule I’m intercepting the requests from “Custom Agent” and sending back a custom Header. This works fine for GET requests, however the data I want to send could potentially exceed the limits allowed for GET requests, so I’d like to use POST (as above).
I have tested this code on a real IIS server and it works, however the Visual Studio development server causes an exception “405 Method Not Allowed” every time GetResponse() is called after POST data is sent (GET works fine.)
Can anyone offer an explanation as to why the development server seems to be rejecting POST requests?
Edits: Question title and body updated to emphasise the problem residing with the VS dev server.
I’ve discovered that the “405 Method Not Allowed” error is returned when POSTing to folder addresses. (Using the question code above)
The VS development server seemingly doesn’t allow;
… but is happy with;
The IIS server produces the error for;
… but is happy with;