Simulating submit button using HttpRequest

F

fahd

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

fahd said:
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
 
F

fahd

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
 
F

fahd

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
 

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