Connecting Tech Pros Worldwide Forums | Help | Site Map

WEBREQUEST AND WEBRESPONSE PROBLEM

Savas Ates
Guest
 
Posts: n/a
#1: Jan 24 '06
I have a vb.net web application. I want to post some variables to another
web page and take some values back and process them.


This is codes in my target url. I should post "langpair" cariable to my
target url and it will respond with a value depend on a value which i send.
<SELECT name=langpair><OPTION
value=en|de>English to German</OPTION><OPTION
value=en|es>English to Spanish</OPTION>
</select>



I must use post method. I must give a value for select object and post it to
my target url ? I heard about BeginGetRequestStream but i dont know how to
do it ?


Dim objURI As Uri = New Uri("http://www.isbuluyorum.com/translate")
Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
objWebRequest.ContentType = "text/html; charset=utf-8"
objWebRequest.Method = "POST"
'BEFORE to get response i should post variables to other web page ?


Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
Dim objStream As Stream = objWebResponse.GetResponseStream()
Dim objStreamReader As StreamReader = New StreamReader(objStream)
Dim strHTML As String = objStreamReader.ReadToEnd

Response.Write(Server.HtmlEncode(strHTML))



Kevin Spencer
Guest
 
Posts: n/a
#2: Jan 24 '06

re: WEBREQUEST AND WEBRESPONSE PROBLEM


The body of an HTTP POST of form data is a set of URL-encoded name=value
pairs. For example, the body of the message you would want to send in this
case would be something like:

langpair=en%7Cde

Note the "%7C" used for the "|" character. For the most part, you can use
HttpUtility.UrlEncode to do the encoding of the string. However, many
servers don't recognize "%20" as a space, so you would want to use "+" for
spaces instead.

You just write the string to the stream you receive from the
GetResponseStream method.

The following example is in C#. I trust you can do the translation:

// ...code to initialize WebRequest and WebResponse...

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
System.IO.Stream requestStream = null;
byte[] body;

objWebRequest.Method = "POST";
objWebRequest.ContentType = "application/x-www-form-urlencoded";
objWebbody = encoding.GetBytes("langpair=en%7Cde");
objWebRequest.ContentLength = body.Length;
requestStream = Request.GetRequestStream();
requestStream.Write(body, 0, body.Length);
objWebResponse = (HttpWebResponse)Request.GetResponse();

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Savas Ates" <in da club> wrote in message
news:u7hfyAOIGHA.2668@tk2msftngp13.phx.gbl...[color=blue]
>I have a vb.net web application. I want to post some variables to another
> web page and take some values back and process them.
>
>
> This is codes in my target url. I should post "langpair" cariable to my
> target url and it will respond with a value depend on a value which i
> send.
> <SELECT name=langpair><OPTION
> value=en|de>English to German</OPTION><OPTION
> value=en|es>English to Spanish</OPTION>
> </select>
>
>
>
> I must use post method. I must give a value for select object and post it
> to
> my target url ? I heard about BeginGetRequestStream but i dont know how
> to
> do it ?
>
>
> Dim objURI As Uri = New Uri("http://www.isbuluyorum.com/translate")
> Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
> objWebRequest.ContentType = "text/html; charset=utf-8"
> objWebRequest.Method = "POST"
> 'BEFORE to get response i should post variables to other web page ?
>
>
> Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
> Dim objStream As Stream = objWebResponse.GetResponseStream()
> Dim objStreamReader As StreamReader = New StreamReader(objStream)
> Dim strHTML As String = objStreamReader.ReadToEnd
>
> Response.Write(Server.HtmlEncode(strHTML))
>
>[/color]


Savas Ates
Guest
 
Posts: n/a
#3: Jan 24 '06

re: WEBREQUEST AND WEBRESPONSE PROBLEM


I wrote the code which is below. It seems ok but i have more variables to
send to my target web page ?
<textarea name=text1> </textarea>
<textarea name=text2> </textarea>

how can i add all variables to send to my target page ?








Dim encoding As New System.Text.ASCIIEncoding

Dim requestStream As System.IO.Stream



Dim objURI As Uri = New Uri(http://www.isbuluyorum.com)

Dim objWebRequest As WebRequest = WebRequest.Create(objURI)

objWebRequest.ContentType = "application/x-www-form-urlencoded"

objWebRequest.Method = "POST"

Dim body() As Byte

body = encoding.GetBytes("langpair=en%7Cde")

objWebRequest.ContentLength = body.Length

requestStream = objWebRequest.GetRequestStream

requestStream.Write(body, 0, body.Length)



Dim objWebResponse As WebResponse = objWebRequest.GetResponse()

Dim objStream As Stream = objWebResponse.GetResponseStream()

Dim objStreamReader As StreamReader = New StreamReader(objStream)

Dim strHTML As String = objStreamReader.ReadToEnd

Response.Write(strHTML)











"Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
news:#WisQvOIGHA.1288@TK2MSFTNGP09.phx.gbl...[color=blue]
> The body of an HTTP POST of form data is a set of URL-encoded name=value
> pairs. For example, the body of the message you would want to send in this
> case would be something like:
>
> langpair=en%7Cde
>
> Note the "%7C" used for the "|" character. For the most part, you can use
> HttpUtility.UrlEncode to do the encoding of the string. However, many
> servers don't recognize "%20" as a space, so you would want to use "+" for
> spaces instead.
>
> You just write the string to the stream you receive from the
> GetResponseStream method.
>
> The following example is in C#. I trust you can do the translation:
>
> // ...code to initialize WebRequest and WebResponse...
>
> System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
> System.IO.Stream requestStream = null;
> byte[] body;
>
> objWebRequest.Method = "POST";
> objWebRequest.ContentType = "application/x-www-form-urlencoded";
> objWebbody = encoding.GetBytes("langpair=en%7Cde");
> objWebRequest.ContentLength = body.Length;
> requestStream = Request.GetRequestStream();
> requestStream.Write(body, 0, body.Length);
> objWebResponse = (HttpWebResponse)Request.GetResponse();
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Who is Mighty Abbott?
> A twin turret scalawag.
>
> "Savas Ates" <in da club> wrote in message
> news:u7hfyAOIGHA.2668@tk2msftngp13.phx.gbl...[color=green]
> >I have a vb.net web application. I want to post some variables to another
> > web page and take some values back and process them.
> >
> >
> > This is codes in my target url. I should post "langpair" cariable to my
> > target url and it will respond with a value depend on a value which i
> > send.
> > <SELECT name=langpair><OPTION
> > value=en|de>English to German</OPTION><OPTION
> > value=en|es>English to Spanish</OPTION>
> > </select>
> >
> >
> >
> > I must use post method. I must give a value for select object and post[/color][/color]
it[color=blue][color=green]
> > to
> > my target url ? I heard about BeginGetRequestStream but i dont know how
> > to
> > do it ?
> >
> >
> > Dim objURI As Uri = New Uri("http://www.isbuluyorum.com/translate")
> > Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
> > objWebRequest.ContentType = "text/html; charset=utf-8"
> > objWebRequest.Method = "POST"
> > 'BEFORE to get response i should post variables to other web page[/color][/color]
?[color=blue][color=green]
> >
> >
> > Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
> > Dim objStream As Stream = objWebResponse.GetResponseStream()
> > Dim objStreamReader As StreamReader = New StreamReader(objStream)
> > Dim strHTML As String = objStreamReader.ReadToEnd
> >
> > Response.Write(Server.HtmlEncode(strHTML))
> >
> >[/color]
>
>[/color]


Kevin Spencer
Guest
 
Posts: n/a
#4: Jan 24 '06

re: WEBREQUEST AND WEBRESPONSE PROBLEM


Ah yes. It works just like a QueryString. Each name=value pair is separated
by the "&" character, as in:

text1=blah+blah+blah&text2=blah+blah+blah

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Savas Ates" <in da club> wrote in message
news:%23odseEPIGHA.604@TK2MSFTNGP14.phx.gbl...[color=blue]
>I wrote the code which is below. It seems ok but i have more variables to
> send to my target web page ?
> <textarea name=text1> </textarea>
> <textarea name=text2> </textarea>
>
> how can i add all variables to send to my target page ?
>
>
>
>
>
>
>
>
> Dim encoding As New System.Text.ASCIIEncoding
>
> Dim requestStream As System.IO.Stream
>
>
>
> Dim objURI As Uri = New Uri(http://www.isbuluyorum.com)
>
> Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
>
> objWebRequest.ContentType = "application/x-www-form-urlencoded"
>
> objWebRequest.Method = "POST"
>
> Dim body() As Byte
>
> body = encoding.GetBytes("langpair=en%7Cde")
>
> objWebRequest.ContentLength = body.Length
>
> requestStream = objWebRequest.GetRequestStream
>
> requestStream.Write(body, 0, body.Length)
>
>
>
> Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
>
> Dim objStream As Stream = objWebResponse.GetResponseStream()
>
> Dim objStreamReader As StreamReader = New StreamReader(objStream)
>
> Dim strHTML As String = objStreamReader.ReadToEnd
>
> Response.Write(strHTML)
>
>
>
>
>
>
>
>
>
>
>
> "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
> news:#WisQvOIGHA.1288@TK2MSFTNGP09.phx.gbl...[color=green]
>> The body of an HTTP POST of form data is a set of URL-encoded name=value
>> pairs. For example, the body of the message you would want to send in
>> this
>> case would be something like:
>>
>> langpair=en%7Cde
>>
>> Note the "%7C" used for the "|" character. For the most part, you can use
>> HttpUtility.UrlEncode to do the encoding of the string. However, many
>> servers don't recognize "%20" as a space, so you would want to use "+"
>> for
>> spaces instead.
>>
>> You just write the string to the stream you receive from the
>> GetResponseStream method.
>>
>> The following example is in C#. I trust you can do the translation:
>>
>> // ...code to initialize WebRequest and WebResponse...
>>
>> System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
>> System.IO.Stream requestStream = null;
>> byte[] body;
>>
>> objWebRequest.Method = "POST";
>> objWebRequest.ContentType = "application/x-www-form-urlencoded";
>> objWebbody = encoding.GetBytes("langpair=en%7Cde");
>> objWebRequest.ContentLength = body.Length;
>> requestStream = Request.GetRequestStream();
>> requestStream.Write(body, 0, body.Length);
>> objWebResponse = (HttpWebResponse)Request.GetResponse();
>>
>> --
>> HTH,
>>
>> Kevin Spencer
>> Microsoft MVP
>> .Net Developer
>> Who is Mighty Abbott?
>> A twin turret scalawag.
>>
>> "Savas Ates" <in da club> wrote in message
>> news:u7hfyAOIGHA.2668@tk2msftngp13.phx.gbl...[color=darkred]
>> >I have a vb.net web application. I want to post some variables to
>> >another
>> > web page and take some values back and process them.
>> >
>> >
>> > This is codes in my target url. I should post "langpair" cariable to
>> > my
>> > target url and it will respond with a value depend on a value which i
>> > send.
>> > <SELECT name=langpair><OPTION
>> > value=en|de>English to German</OPTION><OPTION
>> > value=en|es>English to Spanish</OPTION>
>> > </select>
>> >
>> >
>> >
>> > I must use post method. I must give a value for select object and post[/color][/color]
> it[color=green][color=darkred]
>> > to
>> > my target url ? I heard about BeginGetRequestStream but i dont know
>> > how
>> > to
>> > do it ?
>> >
>> >
>> > Dim objURI As Uri = New Uri("http://www.isbuluyorum.com/translate")
>> > Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
>> > objWebRequest.ContentType = "text/html; charset=utf-8"
>> > objWebRequest.Method = "POST"
>> > 'BEFORE to get response i should post variables to other web
>> > page[/color][/color]
> ?[color=green][color=darkred]
>> >
>> >
>> > Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
>> > Dim objStream As Stream = objWebResponse.GetResponseStream()
>> > Dim objStreamReader As StreamReader = New
>> > StreamReader(objStream)
>> > Dim strHTML As String = objStreamReader.ReadToEnd
>> >
>> > Response.Write(Server.HtmlEncode(strHTML))
>> >
>> >[/color]
>>
>>[/color]
>
>[/color]


Savas Ates
Guest
 
Posts: n/a
#5: Jan 24 '06

re: WEBREQUEST AND WEBRESPONSE PROBLEM


thanks ;)


"Savas Ates" <in da club> wrote in message
news:u7hfyAOIGHA.2668@tk2msftngp13.phx.gbl...[color=blue]
> I have a vb.net web application. I want to post some variables to another
> web page and take some values back and process them.
>
>
> This is codes in my target url. I should post "langpair" cariable to my
> target url and it will respond with a value depend on a value which i[/color]
send.[color=blue]
> <SELECT name=langpair><OPTION
> value=en|de>English to German</OPTION><OPTION
> value=en|es>English to Spanish</OPTION>
> </select>
>
>
>
> I must use post method. I must give a value for select object and post it[/color]
to[color=blue]
> my target url ? I heard about BeginGetRequestStream but i dont know how[/color]
to[color=blue]
> do it ?
>
>
> Dim objURI As Uri = New Uri("http://www.isbuluyorum.com/translate")
> Dim objWebRequest As WebRequest = WebRequest.Create(objURI)
> objWebRequest.ContentType = "text/html; charset=utf-8"
> objWebRequest.Method = "POST"
> 'BEFORE to get response i should post variables to other web page[/color]
?[color=blue]
>
>
> Dim objWebResponse As WebResponse = objWebRequest.GetResponse()
> Dim objStream As Stream = objWebResponse.GetResponseStream()
> Dim objStreamReader As StreamReader = New StreamReader(objStream)
> Dim strHTML As String = objStreamReader.ReadToEnd
>
> Response.Write(Server.HtmlEncode(strHTML))
>
>[/color]


Closed Thread