Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

Simulating submit button using HttpRequest

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fahd - 29 Jul 2008 15:57 GMT
Hi,

I'm trying to run a ccxml, and consequently, a voiceXML page on the
voice server through my application and the documentation of the
server mention that it can be done using HTTP POST.

Following is a sample example of html that works and successfully
starts a ccxml page on a remote destination

<html>
    <body>
        <form method="post" action="http://localhost:4045/basichttp"
enctype="application/x-www-form-urlencoded">
            ID="Form1">
            name: <input type="text" name="name" size="40"
value="OutBoundCallEvent" id="Text1" />

            URI to be contacted by the outbound: <input type="text"
name="ToBeContacted_URI" size="52" value="10.2.50.42" id="Text5" />

            VoiceXML URI: <input type="text" name="VoiceXML_URI" size="73"
value="file:///C:/voiceXML/three.vxml">
                ID="Text6" />

            CCXML URI: <input type="text" name="uri" size="75" value="file:///
C:/CCXML/outboundcall.ccxml">
                ID="Text7" />

            <input type="submit" id="Submit1" name="Submit1">
        </input></input></input></form>
    </body>
</html>

Now I want to achieve the same effect through my c# application and I
am using HttpRequest and HttpResponse to mimic submitting the form
above.

following is my attempt on this

            string uri = @"http://localhost:4045/basichttp";
            string data =
@"name=OutBoundCallEvent&ToBeContacted_URI=10.2.50.42&VoiceXML_URI=file:///
C:/voiceXML/three.vxml&uri=file:///C:/CCXML/outboundcall.ccxml";
            byte [] bytes = System.Text.Encoding.ASCII.GetBytes(data);

            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(uri);
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            req.ContentLength = bytes.Length;

            System.IO.Stream os = req.GetRequestStream();
            os.Write (bytes, 0, bytes.Length);
            os.Close();

            HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
            if (resp== null)
            {
                MessageBox.Show("Problem in sending response!");
            }

            System.IO.StreamReader sr = new
System.IO.StreamReader(resp.GetResponseStream());
            string result = sr.ReadToEnd().Trim();
            MessageBox.Show("All done!");

Now when I try to get the response using HttpResponse I get an error "
The remote server returned an error: (400) Bad Request."

Q1 Does writing the data into the stream means that the data has been
sent just like pressing the submit button? or does using the
getResponse method do this?
Q2 Is there something im missing in order to achieve a simple submit
function? I have tried sending some data over to an asp page and use
response like above and it all seems to work fine.

Any help will be greatly appreciated.
Kind Regards
Fahd
Nicholas Paldino [.NET/C# MVP] - 29 Jul 2008 17:36 GMT
Fahd,

   When you press the Submit button on the form, it encodes the fields in
the form (which may contain hidden fields) and then sends that data to the
server.

   Your best bet here is to get an HTTP request sniffer (look for Fiddler,
it's free, and will do what you want).  Basically, you will be able to issue
the request in your browser of choice, and see how it is sent to the server.
That will help determine the best way to construct the message in code.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
>
[quoted text clipped - 74 lines]
> Kind Regards
> Fahd
fahd - 31 Jul 2008 16:07 GMT
Hi Nicholas,

Thanks for the tip, im on it.
Just one question if you may

does the following piece of code sends the data to the server

                      System.IO.Stream os = req.GetRequestStream();
                       os.Write (bytes, 0, bytes.Length);
                       os.Close();

or this bit sends data to the server
                       HttpWebResponse resp = (HttpWebResponse)
req.GetResponse();

Thanks
Fahd
fahd - 31 Jul 2008 16:37 GMT
Hi Nicholas,

used fiddler and got to know my mistake the dreaded escape
characters!!!
and I found out answer to my question as well. Thanks a million and
Fiddler is a gem :)

take care
Fahd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.