http Post

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();

}
 
J

Joerg Jooss

:

I don't think it's your code, although you're doing weird stuff -- why
do you URL-encode a HTTP body?

Check out the Allow header returned by the 405 response to figure out
whether POSTing to that URL is really not allowed.

Cheers,
 

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

Top