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.Http WebRequest.Crea te(URL);
hReq.Method = "POST";
hReq.ContentTyp e = "applicatio n/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.Index OfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Appe nd(HttpUtility. UrlEncode(paylo ad.Substring(i,
payload.Length-i)));
break;
}
UrlEncoded.Appe nd(HttpUtility. UrlEncode(paylo ad.Substring(i, j-i)));
UrlEncoded.Appe nd(payload.Subs tring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.G etBytes(UrlEnco ded.ToString()) ;
hReq.ContentLen gth = SomeBytes.Lengt h;
Stream newStream = hReq.GetRequest Stream();
newStream.Write (SomeBytes, 0, SomeBytes.Lengt h);
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.Http WebRequest.Crea te(URL);
hReq.Method = "POST";
hReq.ContentTyp e = "applicatio n/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.Index OfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Appe nd(HttpUtility. UrlEncode(paylo ad.Substring(i,
payload.Length-i)));
break;
}
UrlEncoded.Appe nd(HttpUtility. UrlEncode(paylo ad.Substring(i, j-i)));
UrlEncoded.Appe nd(payload.Subs tring(j,1));
i = j+1;
}
SomeBytes = Encoding.UTF8.G etBytes(UrlEnco ded.ToString()) ;
hReq.ContentLen gth = SomeBytes.Lengt h;
Stream newStream = hReq.GetRequest Stream();
newStream.Write (SomeBytes, 0, SomeBytes.Lengt h);
newStream.Close ();
}
Comment