473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HttpWebRequest.GetResponse on POST returns 405 method not allowed


Hello, I've searched the forums and can't find an answer -- if it i
there, kindly point me in that direction.

I would like to simulate a browser POSTing a FORM and be able to pars
the response.

I have the following code in my Page_Load (litResponse is defined a
<ASP:Literal>):
string sURL = "http://localhost/PostToMe.html";
string strPost = "Field1=abc+123&Field2=22"
string result = "";

StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(sURL);
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";

try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
litResponse.Text = string.Format("Error: {0}", e.Message);
}
finally
{
myWriter.Close();
}

HttpWebResponse objResponse
(HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr = ne
StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}
litResponse.Text += string.Format("<hr><pre>{0}</pre><hr>", result);
This matches several examples online in various places, but I get th
following when I attempt to load this page:
The remote server returned an error: (405) Method Not Allowed.
Description: An unhandled exception occurred during the execution o
the current web request. Please review the stack trace for mor
information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returne
an error: (405) Method Not Allowed.

Source Error:

Line 143: }
Line 144:
Line 145: HttpWebResponse objResponse
(HttpWebResponse)objRequest.GetResponse();
Line 146: using (StreamReader sr = ne
StreamReader(objResponse.GetResponseStream()) )
Line 147: {
Source File: c:\inetpub\wwwroot\Test.aspx Line: 145

Stack Trace:

[WebException: The remote server returned an error: (405) Method No
Allowed.]
System.Net.HttpWebRequest.CheckFinalStatus() +676
System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult asyncResult
+139
System.Net.HttpWebRequest.GetResponse() +147
ASP.Test_aspx.Page_Load(Object Source, EventArgs E) i
c:\inetpub\wwwroot\Test.aspx:145
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
Version Information: Microsoft .NET Framework Version:1.1.4322.573
ASP.NET Version:1.1.4322.573
Any assistance with code, permissions, installation problems (I jus
installed the .Net framework 1.1. on this server (IIS 5)) would b
appreciated.

Glenn
glenn_lanier at netzero dot ne

--
GlennLanie
-----------------------------------------------------------------------
GlennLanier's Profile: http://www.highdots.com/forums/member.php?userid=21
View this thread: http://www.highdots.com/forums/showthread.php?t=149581

Nov 19 '05 #1
2 20139
GlennLanier wrote:
Hello, I've searched the forums and can't find an answer -- if it is
there, kindly point me in that direction.

I would like to simulate a browser POSTing a FORM and be able to parse
the response.

I have the following code in my Page_Load (litResponse is defined as
<ASP:Literal>):
string sURL = "http://localhost/PostToMe.html";
string strPost = "Field1=abc+123&Field2=22"
string result = "";

StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(sURL);
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";

try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
litResponse.Text = string.Format("Error: {0}", e.Message);
}
finally
{
myWriter.Close();
}

HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr = new
StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}
litResponse.Text += string.Format("<hr><pre>{0}</pre><hr>", result);
This matches several examples online in various places, but I get the
following when I attempt to load this page:
The remote server returned an error: (405) Method Not Allowed.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned
an error: (405) Method Not Allowed.

Source Error:

Line 143: }
Line 144:
Line 145: HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();
Line 146: using (StreamReader sr = new
StreamReader(objResponse.GetResponseStream()) )
Line 147: {
Source File: c:\inetpub\wwwroot\Test.aspx Line: 145

Stack Trace:

[WebException: The remote server returned an error: (405) Method Not
Allowed.]
System.Net.HttpWebRequest.CheckFinalStatus() +676
System.Net.HttpWebRequest.EndGetResponse(IAsyncRes ult asyncResult)
+139
System.Net.HttpWebRequest.GetResponse() +147
ASP.Test_aspx.Page_Load(Object Source, EventArgs E) in
c:\inetpub\wwwroot\Test.aspx:145
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
Version Information: Microsoft .NET Framework Version:1.1.4322.573;
ASP.NET Version:1.1.4322.573
Any assistance with code, permissions, installation problems (I just
installed the .Net framework 1.1. on this server (IIS 5)) would be
appreciated.

Glenn
glenn_lanier at netzero dot net


A server can refuse to accept a POST request for a certain page, only a
GET. Perhaps they can also do so based on referrer, so outside sites
cannot POST into the site.

Have you verified all of this is not true? Also verify you are POSTing
to the correct URL, and that all fields that are expected by the
receiving site are included in your form.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 19 '05 #2

Craig Deelsnyder Wrote:

A server can refuse to accept a POST request for a certain page, onl
a
GET. Perhaps they can also do so based on referrer, so outside sites
cannot POST into the site.

I can POST to this page normally, the URL specified localhost whic
resolved to 127.0.0.1. When I changed it to the actual IP of m
machine, it worked. Not sure why, because I can (and usually do) acces
the machine using http://localhost/. Since this is a development server
I modified the code slightly:
string sServer = Request.ServerVariables["SERVER_NAME"];
string sURL = string.Format(@"http://{0}/PostToMe.html", sServer);
Craig Deelsnyder Wrote:
Have you verified all of this is not true? Also verify you ar
POSTing
to the correct URL, and that all fields that are expected by the
receiving site are included in your form.


All of this is correct -- the page doesn't require any variables, an
simply does a dump of all variables it receives -- great for testing!

I have worked around the problem by specifying a server name, but woul
love to know why localhost didn't work.

Thanks,
Glen

--
GlennLanie
-----------------------------------------------------------------------
GlennLanier's Profile: http://www.highdots.com/forums/member.php?userid=21
View this thread: http://www.highdots.com/forums/showthread.php?t=149581

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jeff B | last post by:
I'm trying to create a simple screen scraping application and I kept getting a System.Net.WebException thrown back with a message of "The operation has timed-out." At first I thought it was some...
5
by: japslam japslam via DotNetMonster.com | last post by:
Hi all, I have problem when I use HttpWebRequest and take long time to call to my service server. If at that time there are many request comes in semultaneous, I will get this exception ...
2
by: Steve Richter | last post by:
I have a page that uses simple HTTP GET to do an ISBN lookup via Amazon.com. The page works when I run it from //localhost. But I have moved it to my godaddy.com shared hoster site, and I get...
0
by: Veerle | last post by:
Hi, On the website of the Belgian lottery, you can download an excel sheet with lottery results (the winning numbers) over the years and an excel sheet with financial results (the winnings) over...
1
by: MikeZ | last post by:
I post this question last week, no good answer, so I post again. Sorry about this. I use WebRequest.Create/WebRequest.GetResponse to handle HTTP request in a VS2003 project. I got HTTP violation...
1
by: Morgan Cheng | last post by:
When the function HttpWebRequest.GetResponse() is called, what happened? I mean, does this function return till all HTTP response is downloaded to local machine? or only HTTP header part retrived...
5
by: mr.newsgroupguy | last post by:
I am working in C# .NET 1.1. My app has a button on its main form that checks to see if it has access to a file on our server, just an XML file. On our server we are running W2K IIS with a...
0
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302...
4
by: =?Utf-8?B?SmltIE93ZW4=?= | last post by:
Hi, I've run into a set of errors I don't understand coming back from HttpWebRequest.GetResponse, In one case, null is returned from the request without an Exception and in the other the request...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.