HttpWebRequest.GetResponse on POST returns 405 method not allowed

G

GlennLanier

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(IAsyncResult 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
 
C

Craig Deelsnyder

GlennLanier said:
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(IAsyncResult 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.
 
G

GlennLanier

Craig said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top