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
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