http Post

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wobbly Bob

    #1

    http Post

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

    }


  • Joerg Jooss

    #2
    Re: http Post

    "Wobbly Bob" wrote:

    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,
    --
    Joerg Jooss
    joerg.jooss@gmx .net

    Comment

    Working...