|
 |
aspx thread: HttpWebRequest and HttpWebResponse
Message #1 by <wrox@d...> on Sun, 21 Oct 2001 10:46:32 +0100
|
|
Hi,
I hope someone can help! I am trying to use HttpWebRequest and
HttpWebResponse to retrieve information from a website, this has been
working fine for sites. However I know want to post information to a
form and retrieve the results.
I have tried the code below but all I have achieved is bit of hair loss!
string Parameters = "";
Parameters += "search=" +
HttpContext.Current.Server.UrlEncode("foo") + "&";
Parameters += "duration=" +
HttpContext.Current.Server.UrlEncode("5days");
string CommandURI = "http://localhost/search.aspx";
HttpWebRequest MyWebRequest = (HttpWebRequest)
WebRequest.Create(CommandURI);
MyWebRequest.Referer = "http://localhost";
MyWebRequest.ContentType
"application/x-www-form-urlencoded";
MyWebRequest.Method = "POST";
MyWebRequest.PreAuthenticate=true;
byte [] bytes
System.Text.Encoding.ASCII.GetBytes(Parameters);
MyWebRequest.ContentLength = bytes.Length;
Stream OutputStream = MyWebRequest.GetRequestStream
();
OutputStream.Write (bytes, 0, bytes.Length);
OutputStream.Close ();
HttpWebResponse MyWebResponse = (HttpWebResponse)
MyWebRequest.GetResponse();
Stream MyStream = MyWebResponse.GetResponseStream();
StreamReader MyStreamReader = new
StreamReader(MyStream);
this.Response.Write(
MyStreamReader.ReadToEnd().Trim());
Any suggestions ?
Cheers
David
Message #2 by "Ollie Cornes" <lotsofemail@c...> on Sun, 21 Oct 2001 11:50:40 +0100
|
|
attached is a method i wrote to post a string of parameters to a gateway
using HTTP.
any help?
Ollie
/// <summary>
/// Requests a URI and returns the response as a string.
/// </summary>
private string GetURI(string URI, string Parameters)
{
string CommandURI = URI;
WebRequest MyWebRequest = WebRequestFactory.Create(CommandURI);
MyWebRequest.ContentType = "application/x-www-form-urlencoded";
MyWebRequest.Method = "POST";
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
MyWebRequest.ContentLength = bytes.Length;
Stream OutputStream = MyWebRequest.GetRequestStream ();
OutputStream.Write (bytes, 0, bytes.Length);
OutputStream.Close ();
WebResponse MyWebResponse = MyWebRequest.GetResponse();
Stream MyStream = MyWebResponse.GetResponseStream();
StreamReader MyStreamReader = new StreamReader(MyStream);
return MyStreamReader.ReadToEnd().Trim();
}
----- Original Message -----
From: <wrox@d...>
To: "ASP+" <aspx@p...>
Sent: Sunday, October 21, 2001 10:46 AM
Subject: [aspx] HttpWebRequest and HttpWebResponse
>
> Hi,
>
> I hope someone can help! I am trying to use HttpWebRequest and
> HttpWebResponse to retrieve information from a website, this has been
> working fine for sites. However I know want to post information to a
> form and retrieve the results.
>
> I have tried the code below but all I have achieved is bit of hair loss!
>
> string Parameters = "";
> Parameters += "search=" +
> HttpContext.Current.Server.UrlEncode("foo") + "&";
> Parameters += "duration=" +
> HttpContext.Current.Server.UrlEncode("5days");
> string CommandURI = "http://localhost/search.aspx";
> HttpWebRequest MyWebRequest = (HttpWebRequest)
> WebRequest.Create(CommandURI);
> MyWebRequest.Referer = "http://localhost";
> MyWebRequest.ContentType
> "application/x-www-form-urlencoded";
> MyWebRequest.Method = "POST";
> MyWebRequest.PreAuthenticate=true;
> byte [] bytes
> System.Text.Encoding.ASCII.GetBytes(Parameters);
> MyWebRequest.ContentLength = bytes.Length;
>
> Stream OutputStream = MyWebRequest.GetRequestStream
> ();
> OutputStream.Write (bytes, 0, bytes.Length);
> OutputStream.Close ();
> HttpWebResponse MyWebResponse = (HttpWebResponse)
> MyWebRequest.GetResponse();
> Stream MyStream = MyWebResponse.GetResponseStream();
> StreamReader MyStreamReader = new
> StreamReader(MyStream);
> this.Response.Write(
> MyStreamReader.ReadToEnd().Trim());
>
> Any suggestions ?
>
> Cheers
>
> David
>
>
Message #3 by <wrox@d...> on Mon, 22 Oct 2001 20:45:07 +0100
|
|
Thanks it works a dream - however still having a but of difficulty
maintaining session with the Stream object also WebRequestFactory is no
longer available in beta2.
Thanks
David
-----Original Message-----
From: Ollie Cornes [mailto:lotsofemail@c...]
Sent: 21 October 2001 11:51
To: ASP+
Subject: [aspx] Re: HttpWebRequest and HttpWebResponse
attached is a method i wrote to post a string of parameters to a gateway
using HTTP.
any help?
Ollie
/// <summary>
/// Requests a URI and returns the response as a string.
/// </summary>
private string GetURI(string URI, string Parameters)
{
string CommandURI = URI;
WebRequest MyWebRequest = WebRequestFactory.Create(CommandURI);
MyWebRequest.ContentType = "application/x-www-form-urlencoded";
MyWebRequest.Method = "POST";
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
MyWebRequest.ContentLength = bytes.Length;
Stream OutputStream = MyWebRequest.GetRequestStream ();
OutputStream.Write (bytes, 0, bytes.Length);
OutputStream.Close ();
WebResponse MyWebResponse = MyWebRequest.GetResponse();
Stream MyStream = MyWebResponse.GetResponseStream();
StreamReader MyStreamReader = new StreamReader(MyStream);
return MyStreamReader.ReadToEnd().Trim();
}
----- Original Message -----
From: <wrox@d...>
To: "ASP+" <aspx@p...>
Sent: Sunday, October 21, 2001 10:46 AM
Subject: [aspx] HttpWebRequest and HttpWebResponse
>
> Hi,
>
> I hope someone can help! I am trying to use HttpWebRequest and
> HttpWebResponse to retrieve information from a website, this has been
> working fine for sites. However I know want to post information to a
> form and retrieve the results.
>
> I have tried the code below but all I have achieved is bit of hair
loss!
>
> string Parameters = "";
> Parameters += "search=" +
> HttpContext.Current.Server.UrlEncode("foo") + "&";
> Parameters += "duration=" +
> HttpContext.Current.Server.UrlEncode("5days");
> string CommandURI = "http://localhost/search.aspx";
> HttpWebRequest MyWebRequest = (HttpWebRequest)
> WebRequest.Create(CommandURI);
> MyWebRequest.Referer = "http://localhost";
> MyWebRequest.ContentType
> "application/x-www-form-urlencoded";
> MyWebRequest.Method = "POST";
> MyWebRequest.PreAuthenticate=true;
> byte [] bytes
> System.Text.Encoding.ASCII.GetBytes(Parameters);
> MyWebRequest.ContentLength = bytes.Length;
>
> Stream OutputStream = MyWebRequest.GetRequestStream
> ();
> OutputStream.Write (bytes, 0, bytes.Length);
> OutputStream.Close ();
> HttpWebResponse MyWebResponse = (HttpWebResponse)
> MyWebRequest.GetResponse();
> Stream MyStream = MyWebResponse.GetResponseStream();
> StreamReader MyStreamReader = new
> StreamReader(MyStream);
> this.Response.Write(
> MyStreamReader.ReadToEnd().Trim());
>
> Any suggestions ?
>
> Cheers
>
> David
|
|
 |