Search Amazon : HttpWebRequest « Network « ASP.Net






Search Amazon


<%@ Page %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<script language="C#" runat="server">

private void SubmitButton_Click(System.Object sender, System.EventArgs e){
  String RequestUrl = "http://www.amazon.com/exec/obidos/search-handle-form";
  String Post = "field-keywords=" + SearchText.Text;
  StreamWriter Writer = null;

  HttpWebRequest webRequestObject = null;
  StreamReader sr = null;
  HttpWebResponse webResponseObject = null;
  
  try{

    webRequestObject = (HttpWebRequest) WebRequest.Create(RequestUrl);
    webRequestObject.Method = "POST";
    webRequestObject.ContentType = "application/x-www-form-urlencoded";
    webRequestObject.ContentLength = Post.Length;
    Writer = new StreamWriter(webRequestObject.GetRequestStream());
    Writer.Write(Post);
    Writer.Close();

    webResponseObject = (HttpWebResponse) webRequestObject.GetResponse();
    sr = new StreamReader(webResponseObject.GetResponseStream());
    
    String Results = sr.ReadToEnd();
    WebResponseLabel.Text = Results;
    WebResponseText.Text = Results;
  }
  finally{
    try{
      sr.Close();
      webResponseObject.Close();
      webRequestObject.Abort();
    }
    catch{}
  }
}

</script>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <form runat="server" ID="Form1">
      Search Amazon for:
      <asp:TextBox Runat="server" ID="SearchText" Text="Java" />
      <asp:Button Runat="server" ID="SubmitButton" Text="Get Amazon.com Search Results" OnClick="SubmitButton_Click" /><br>
      <asp:TextBox Runat="server" id="WebResponseText" Width="780" Height="300" TextMode="MultiLine" />
      <asp:Label Runat="server" ID="WebResponseLabel" />
    </form>
  </body>
</html>

 








Related examples in the same category

1.Read Stream from HttpWebRequest
2.Read Cookie value from HttpWebRequest
3.Set UserAgent for HttpWebRequest
4.HttpWebRequest AllowAutoRedirect
5.Header of HttpWebRequest