473,839 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cre ate(sURL);
objRequest.Meth od = "POST";
objRequest.Cont entLength = strPost.Length;
objRequest.Cont entType = "applicatio n/x-www-form-urlencoded";

try
{
myWriter = new StreamWriter(ob jRequest.GetReq uestStream());
myWriter.Write( strPost);
}
catch (Exception e)
{
litResponse.Tex t = string.Format(" Error: {0}", e.Message);
}
finally
{
myWriter.Close( );
}

HttpWebResponse objResponse
(HttpWebRespons e)objRequest.Ge tResponse();
using (StreamReader sr = ne
StreamReader(ob jResponse.GetRe sponseStream()) )
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}
litResponse.Tex t += 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.WebE xception: The remote server returne
an error: (405) Method Not Allowed.

Source Error:

Line 143: }
Line 144:
Line 145: HttpWebResponse objResponse
(HttpWebRespons e)objRequest.Ge tResponse();
Line 146: using (StreamReader sr = ne
StreamReader(ob jResponse.GetRe sponseStream()) )
Line 147: {
Source File: c:\inetpub\wwwr oot\Test.aspx Line: 145

Stack Trace:

[WebException: The remote server returned an error: (405) Method No
Allowed.]
System.Net.Http WebRequest.Chec kFinalStatus() +676
System.Net.Http WebRequest.EndG etResponse(IAsy ncResult asyncResult
+139
System.Net.Http WebRequest.GetR esponse() +147
ASP.Test_aspx.P age_Load(Object Source, EventArgs E) i
c:\inetpub\wwwr oot\Test.aspx:1 45
System.Web.UI.C ontrol.OnLoad(E ventArgs e) +67
System.Web.UI.C ontrol.LoadRecu rsive() +35
System.Web.UI.P age.ProcessRequ estMain() +731
Version Information: Microsoft .NET Framework Version:1.1.432 2.573
ASP.NET Version:1.1.432 2.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 20180
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.Cre ate(sURL);
objRequest.Meth od = "POST";
objRequest.Cont entLength = strPost.Length;
objRequest.Cont entType = "applicatio n/x-www-form-urlencoded";

try
{
myWriter = new StreamWriter(ob jRequest.GetReq uestStream());
myWriter.Write( strPost);
}
catch (Exception e)
{
litResponse.Tex t = string.Format(" Error: {0}", e.Message);
}
finally
{
myWriter.Close( );
}

HttpWebResponse objResponse =
(HttpWebRespons e)objRequest.Ge tResponse();
using (StreamReader sr = new
StreamReader(ob jResponse.GetRe sponseStream()) )
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}
litResponse.Tex t += 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.WebE xception: The remote server returned
an error: (405) Method Not Allowed.

Source Error:

Line 143: }
Line 144:
Line 145: HttpWebResponse objResponse =
(HttpWebRespons e)objRequest.Ge tResponse();
Line 146: using (StreamReader sr = new
StreamReader(ob jResponse.GetRe sponseStream()) )
Line 147: {
Source File: c:\inetpub\wwwr oot\Test.aspx Line: 145

Stack Trace:

[WebException: The remote server returned an error: (405) Method Not
Allowed.]
System.Net.Http WebRequest.Chec kFinalStatus() +676
System.Net.Http WebRequest.EndG etResponse(IAsy ncResult asyncResult)
+139
System.Net.Http WebRequest.GetR esponse() +147
ASP.Test_aspx.P age_Load(Object Source, EventArgs E) in
c:\inetpub\wwwr oot\Test.aspx:1 45
System.Web.UI.C ontrol.OnLoad(E ventArgs e) +67
System.Web.UI.C ontrol.LoadRecu rsive() +35
System.Web.UI.P age.ProcessRequ estMain() +731
Version Information: Microsoft .NET Framework Version:1.1.432 2.573;
ASP.NET Version:1.1.432 2.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.ServerV ariables["SERVER_NAM E"];
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
12619
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 kind of connectivity issue on the machine. It didn't make sense though, because I can open up a browser on the same machine and easily browse the web. I'm stumped. I looked over my code for any errors and just couldn't find what I was doing...
5
5881
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 System.Net.WebException: The underlying connection was closed: The request was canceled. ---> System.IO.IOException: Unable to read data from the transport connection. ---> System.ObjectDisposedException: Cannot access a disposed object named...
2
14101
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 errors on the HttpWebRequest.GetResponse statement. The remote server returned an error: (401) Unauthorized also, when I use the network credentials object in the context of my request, I get this error:
0
1380
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 the year. The page is http://www.lotto.be/pages/show.aspx?Culture=nl&pageid=results/download/lotto (in Dutch) or http://www.lotto.be/pages/show.aspx?Culture=en&pageid=results/download/lotto (in English) I want to write a Windows Service in...
1
7001
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 exception on some of the machine. So I did the research, and found to put <useUnsafeHeaderParsing ="true"> in app.config can fix the problem. The machines have framework 1.1 installed, even useUnsafeHeaderParsing is an attribute of version 2.0 ...
1
1867
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 is enough for the function to return? Thanks
5
4874
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 virtual directory, set to Windows Authentication. I am creating an HTTPWebRequest object on the client, and setting its Credentials to CredentialCache.DefaultCredentials. Also, I have tried numerous Timeout settings, but they have not made a...
0
12807
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 problem. I'm using this code in a ASP.NET 2.0 application just in case that matters, maybe someone knows a better way to do this?
4
5201
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 does not appear to leave my system yet still returns the 500 error. In the code below, there is a xml-snippet that I use as a test. When I run the test using the snippet, the server on the other side logs and processes the request correctly...
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10908
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10586
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10293
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.