Results 1 to 5 of 5

Thread: How to make a POST request

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009

    How to make a POST request

    Hi. I am using visual studio 8, using I am trying without success to make a post request to a web form as shown below.

    <?xml version="1.0" encoding="UTF-8"?><postBetOrder xmlns="urn:betfair:games:api:v1"marketId="2568540" round="1" currency="GBP"><betPlace><bidType>BACK</bidType><price>2.5</price><size>2.50</size><selectionId>658440</selectionId></betPlace>

    I encode the information using 'HttpUtility.UrlEncode' and I use
    'myWebRequest.ContentLength = bytedata.Length' to get the length and then I use
    postStream = myWebRequest.GetRequestStream()
    postStream.Write(bytedata, 0, bytedata.Length)
    to post, but keep getting a 'premature end of file' error.
    I dont think that I am posting the right information. Am i supposed to post just the information or do I post the the tags and the information? This is all very new to me and I really am stuck so if anyone can help I'd be most grateful. Thanks for all and any contributions.

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002

    Re: How to make a POST request

    This code (in c# but you should be able to understand) does what you want:
    byte[] data = StrToByteArray(xml);
                response = string.Empty;
                WebRequest webrequest = WebRequest.Create(adress);
                webrequest.Credentials = new NetworkCredential(username, password);
                webrequest.Method = "POST";
                webrequest.ContentLength = data.Length;
                webrequest.ContentType = "text/plain";
                Stream dataStream = webrequest.GetRequestStream();
                dataStream.Write(data, 0, data.Length);
                WebResponse webresponse = webrequest.GetResponse();
                dataStream = webresponse.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                response = reader.ReadToEnd()
    This is the StrToByteArray method:
            private static byte[] StrToByteArray(string str)
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                return encoding.GetBytes(str);
    VB6 & C# (WCF LINQ) mostly

    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    The Granite City

    Re: How to make a POST request


    smileyc are you actually creating a Web site using ASP.Net?

    HttpUtility is part of the System.Web Namespace which would lead me to suspect that you are creating a web site, not a windows form application. Is that correct?

    Can you show all the code that you are using?


  4. #4

    Thread Starter
    New Member
    Join Date
    May 2009

    Re: How to make a POST request

    What I am attempting to do is create a windows form app that uses betfairs api. Its all v new to me, but the GET side of the app is just about done and working ok.I can log on, pass the credential check and get all the game info in real time and have it displayed on a form. But what I can't do is the POST side of things.

    Even with the POST stuff I can log on and pass the credential check but after that I am completely at sea. What I am trying to do is make a back bet. The url is
    And the schema is as in my first post. I know its got to be UTF8 encoded, but after that I am pretty much lost as to what data to include in the post request, for instance do I include the tags, or does the data just get put on the form as per the schema order and if its the wrong data or data type then it just fails? And the data for marketID, round= and currency, I have no idea how to include that in the POST stream. As for posting the code that I have so far, well really I just have a jumble of code, as I try and order and reorder the data for the stream. So, from what I have said here hopefully you have enough to appreciate how dire is my need and my lack of knowledge. Again, thank you for all and any help you can give.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2009

    Re: How to make a POST request

    PS to my earlier reply.
    I use HttpUtility.UrlEncode simply because in looking on the net for answers I found some code that was doing something similar to what I want and it used that form.

    And, the closest to success I have yet managed is the error,

    <?xml-stylesheet type="text/xsl" href="/rest/apiStylesheet.xsl" ?>
    <error xmlns="urn:betfair:games:api:v1">
    <errorDetails>Content is not allowed in prolog.</errorDetails>

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width