W
Wobbly Bob
Hi,
I'm trying to write a program to test an asp based web site. I need to mimic
form data being posted to a page and have tried the following code, based on
the documentation. I get this error:
The remote server returned an error: (405) Method Not Allowed.
The original web page does a form post so I don't think posting is disabled
on the server. Anyone know where I'm going wrong?
Rob.
try
{
hReq = (HttpWebRequest) System.Net.HttpWebRequest.Create(URL);
hReq.Method = "POST";
hReq.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;
if (payload != null)
{
int i=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
payload.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i)));
UrlEncoded.Append(payload.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
hReq.ContentLength = SomeBytes.Length;
Stream newStream = hReq.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
I'm trying to write a program to test an asp based web site. I need to mimic
form data being posted to a page and have tried the following code, based on
the documentation. I get this error:
The remote server returned an error: (405) Method Not Allowed.
The original web page does a form post so I don't think posting is disabled
on the server. Anyone know where I'm going wrong?
Rob.
try
{
hReq = (HttpWebRequest) System.Net.HttpWebRequest.Create(URL);
hReq.Method = "POST";
hReq.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;
if (payload != null)
{
int i=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i,
payload.Length-i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i)));
UrlEncoded.Append(payload.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
hReq.ContentLength = SomeBytes.Length;
Stream newStream = hReq.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}