How to send data to GET method in HTML using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sid0404
    New Member
    • Jul 2008
    • 16

    How to send data to GET method in HTML using c#

    Hi

    I am new to the visualstudio.ne t

    I am trying to develop an application which requires me to send data to a HTML webpage - http://patft.uspto.gov/netahtml/PTO/search-bool.html

    and the user sendsd the data to the webpage and receives the response to that particular values from the webpage.

    I am trying to use httpwebrequest for this purpose, but I am receiving errors, kindly let me know where I am going wrong. When I llok at the source code of the form I realize that it has a GET method and I am trying to sned values to that GET method through my application (windowsforms - c sharp) and is npt able to send it.

    ------------------------------------------------------------------------------------------------
    My code is as follows:
    ------------------------------------------------------------------------------------------------
    [code=c#]
    public Form2(TextBox t)
    {
    InitializeCompo nent();

    label1.Text = t.Text;

    StringBuilder sb = new StringBuilder() ;
    byte[] buf = new byte[100000000];



    try
    {
    string webpage_data = t.Text + "&FIELD1=AS NM";
    // string webpage_data = "TERM1=" + t.Text + "&FIELD1=AS NM";
    // string webpage = "http://patft.uspto.gov/netacgi/nph-Parser";
    string webpage = "http://patft.uspto.gov/netahtml/PTO/search-bool.html";

    ASCIIEncoding encoding = new ASCIIEncoding() ;
    byte[] data = encoding.GetByt es(webpage_data );
    // webpage = webpage + "?" + webpage_data;
    HttpWebRequest myRequest = (HttpWebRequest )WebRequest.Cre ate(webpage);

    myRequest.Crede ntials = CredentialCache .DefaultCredent ials;


    myRequest.Conte ntLength = data.Length;
    myRequest.Metho d = "GET";
    myRequest.Conte ntType = "applicatio n/x-www-form-urlencoded";
    myRequest.Conte ntLength = data.Length;

    Stream newStream = myRequest.GetRe questStream();

    newStream.Write (data, 0, data.Length);

    HttpWebResponse response = (HttpWebRespons e)myRequest.Get Response();
    Stream receive_stream = response.GetRes ponseStream();

    StreamReader read_stream = new StreamReader(re ceive_stream, Encoding.UTF8);

    string str = read_stream.Rea dToEnd();

    newStream.Close ();


    // response.GetRes ponseStream(rea d_stream.ReadTo End());

    response.Close( );
    read_stream.Clo se();

    richTextBox1.Te xt = str;

    }
    catch (Exception)
    {

    throw;
    }
    }
    [/code]
    ---------------------------------------------------------------------------------------------------------------------------------------------------

    and the log at the output tab is as follows:

    ---------------------------------------------------------------------------------------------------------------------------------------------------

    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_32\ms corlib\2.0.0.0_ _b77a5c561934e0 89\mscorlib.dll '
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ Microsoft.Visua lStudio.Hosting Process.Utiliti es\9.0.0.0__b03 f5f7f11d50a3a\M icrosoft.Visual Studio.HostingP rocess.Utilitie s.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Windows. Forms\2.0.0.0__ b77a5c561934e08 9\System.Window s.Forms.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System\2.0.0.0_ _b77a5c561934e0 89\System.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Drawing\ 2.0.0.0__b03f5f 7f11d50a3a\Syst em.Drawing.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ Microsoft.Visua lStudio.Hosting Process.Utiliti es.Sync\9.0.0.0 __b03f5f7f11d50 a3a\Microsoft.V isualStudio.Hos tingProcess.Uti lities.Sync.dll '
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ Microsoft.Visua lStudio.Debugge r.Runtime\9.0.0 .0__b03f5f7f11d 50a3a\Microsoft .VisualStudio.D ebugger.Runtime .dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Users\Siddh arth\Documents\ Visual Studio 2008\Projects\W indowsFormsAppl ication1\Window sFormsApplicati on1\bin\Debug\W indowsFormsAppl ication1.vshost .exe'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Core\3.5 .0.0__b77a5c561 934e089\System. Core.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Xml.Linq \3.5.0.0__b77a5 c561934e089\Sys tem.Xml.Linq.dl l'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Data.Dat aSetExtensions\ 3.5.0.0__b77a5c 561934e089\Syst em.Data.DataSet Extensions.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_32\Sy stem.Data\2.0.0 .0__b77a5c56193 4e089\System.Da ta.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Deployme nt\2.0.0.0__b03 f5f7f11d50a3a\S ystem.Deploymen t.dll'
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Xml\2.0. 0.0__b77a5c5619 34e089\System.X ml.dll'
    The thread 0x11f0 has exited with code 0 (0x0).
    The thread 0x129c has exited with code 0 (0x0).
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Users\Siddh arth\Documents\ Visual Studio 2008\Projects\W indowsFormsAppl ication1\Window sFormsApplicati on1\bin\Debug\W indowsFormsAppl ication1.exe', Symbols loaded.
    'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Configur ation\2.0.0.0__ b03f5f7f11d50a3 a\System.Config uration.dll'
    A first chance exception of type 'System.Net.Pro tocolViolationE xception' occurred in System.dll
    A first chance exception of type 'System.Net.Pro tocolViolationE xception' occurred in WindowsFormsApp lication1.exe
    An unhandled exception of type 'System.Net.Pro tocolViolationE xception' occurred in WindowsFormsApp lication1.exe

    Additional information: Cannot send a content-body with this verb-type.

    The program '[3816] WindowsFormsApp lication1.vshos t.exe: Managed' has exited with code -805306369 (0xcfffffff).
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    Thanks

    Reply awaited

    Sid
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Well, as the error message says, if you are using the GET method, there is no content data.
    Perhaps you wanted the POST method?

    Comment

    • sid0404
      New Member
      • Jul 2008
      • 16

      #3
      Originally posted by Plater
      Well, as the error message says, if you are using the GET method, there is no content data.
      Perhaps you wanted the POST method?
      I changed my method to the POST and now I am getting the output as the source code of the http://patft.uspto.gov/netahtml/PTO/search-bool.html while I need the output as the HTML code of the web page which is generate when you click on the submit button of the "http://patft.uspto.gov/netahtml/PTO/search-bool.html" web page and not the form itself as this web page does not have any POST method so the code runs fine and gives the output as the source code of this webpage only, so the problem remains the same I can not get the output of the next page which shows the results when you hit the submit button.

      The URL used by that webpage is :

      http://patft.uspto.gov/netacgi/nph-Parser

      do you think I should use that directly instead f the URL i am using , if I use this then I get the System.Net.WebE xception at the line

      HttpWebResponse response = (HttpWebRespons e)myRequest.Get Response();

      I think I need to use the
      http://patft.uspto.gov/netacgi/nph-Parser

      but why am I getting the error message, am i missing something obvious ?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Ok well then you should be using POST to go to http://patft.uspto.gov//netacgi/nph-Parser

        Since that is where the Search button goes to.
        You will also probably need to make sure that you have all the correct hidden fields supplied in the content as well.

        Comment

        • sid0404
          New Member
          • Jul 2008
          • 16

          #5
          Originally posted by Plater
          Ok well then you should be using POST to go to http://patft.uspto.gov//netacgi/nph-Parser

          Since that is where the Search button goes to.
          You will also probably need to make sure that you have all the correct hidden fields supplied in the content as well.

          This is what my code look so far, I have issued all the values to the hidden parameters as well, but i think for some reason it is not able to get the response from the server , as i still get the system.net.webe xception, the code is

          ----------------------------------------------------------------------------
          Code:
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.Data;
              using System.IO;
          using System.Net;
              using System.Drawing;
              using System.Linq;
              using System.Text;
              using System.Windows.Forms;
          
              namespace WindowsFormsApplication1
              {
                  public partial class Form2 : Form
                  {
                      public Form2(TextBox t)
                      {
                          InitializeComponent();
          
                          label1.Text = t.Text;
          
                          StringBuilder sb = new StringBuilder();
                          byte[] buf = new byte[100000000];
          
                          string webpage_data = "&TERM1=google&FIELD1=ASNM&co1=AND&TERM2=&FIELD2=&d=PTXT";
                          string webpage = "http://patft.uspto.gov//netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=0&f=S&l=50";
                    //             string webpage = "http://patft.uspto.gov/netahtml/PTO/search-bool.html";
          
                              ASCIIEncoding encoding = new ASCIIEncoding();
                              byte[] data = encoding.GetBytes(webpage_data);
                              webpage = webpage + "?"+ webpage_data;
                              HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webpage);
          
                              myRequest.Credentials = CredentialCache.DefaultCredentials;
                              myRequest.Method = "POST";
          
              /*            
                              myRequest.ContentLength = data.Length;
                              myRequest.Method = "POST";
                              myRequest.ContentType = "application/x-www-form-urlencoded";
                              myRequest.ContentLength = data.Length;   
          
                              Stream newStream = myRequest.GetRequestStream();
          
                              newStream.Write(data, 0, data.Length);
          */                
                              HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
                              Stream receive_stream = response.GetResponseStream();
          
                              StreamReader read_stream = new StreamReader(receive_stream, Encoding.UTF8);
          
                              string str = read_stream.ReadToEnd();
          
                  //            newStream.Close();
          
          
                              //            response.GetResponseStream(read_stream.ReadToEnd());
          
                              response.Close();
                              read_stream.Close();
          
                              richTextBox1.Text = str;
          
              //            }
                      }
          
                      private void label1_Click(object sender, EventArgs e)
                      {
          
                      }
          
                      private void richTextBox1_TextChanged(object sender, EventArgs e)
                      {
          
                      }
                  }
              }
          ----------------------------------------------------------------------------
          while the log on the output tab looks like this :
          ----------------------------------------------------------------------------
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_32\ms corlib\2.0.0.0_ _b77a5c561934e0 89\mscorlib.dll '
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ Microsoft.Visua lStudio.Hosting Process.Utiliti es\9.0.0.0__b03 f5f7f11d50a3a\M icrosoft.Visual Studio.HostingP rocess.Utilitie s.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Windows. Forms\2.0.0.0__ b77a5c561934e08 9\System.Window s.Forms.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System\2.0.0.0_ _b77a5c561934e0 89\System.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Drawing\ 2.0.0.0__b03f5f 7f11d50a3a\Syst em.Drawing.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ Microsoft.Visua lStudio.Hosting Process.Utiliti es.Sync\9.0.0.0 __b03f5f7f11d50 a3a\Microsoft.V isualStudio.Hos tingProcess.Uti lities.Sync.dll '
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ Microsoft.Visua lStudio.Debugge r.Runtime\9.0.0 .0__b03f5f7f11d 50a3a\Microsoft .VisualStudio.D ebugger.Runtime .dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Users\Siddh arth\Documents\ Visual Studio 2008\Projects\W indowsFormsAppl ication1\Window sFormsApplicati on1\bin\Debug\W indowsFormsAppl ication1.vshost .exe'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Core\3.5 .0.0__b77a5c561 934e089\System. Core.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Xml.Linq \3.5.0.0__b77a5 c561934e089\Sys tem.Xml.Linq.dl l'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Data.Dat aSetExtensions\ 3.5.0.0__b77a5c 561934e089\Syst em.Data.DataSet Extensions.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_32\Sy stem.Data\2.0.0 .0__b77a5c56193 4e089\System.Da ta.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Deployme nt\2.0.0.0__b03 f5f7f11d50a3a\S ystem.Deploymen t.dll'
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Xml\2.0. 0.0__b77a5c5619 34e089\System.X ml.dll'
          The thread 0x9d4 has exited with code 0 (0x0).
          The thread 0x10b8 has exited with code 0 (0x0).
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Users\Siddh arth\Documents\ Visual Studio 2008\Projects\W indowsFormsAppl ication1\Window sFormsApplicati on1\bin\Debug\W indowsFormsAppl ication1.exe', Symbols loaded.
          A first chance exception of type 'System.IO.File NotFoundExcepti on' occurred in mscorlib.dll
          'WindowsFormsAp plication1.vsho st.exe' (Managed): Loaded 'C:\Windows\ass embly\GAC_MSIL\ System.Configur ation\2.0.0.0__ b03f5f7f11d50a3 a\System.Config uration.dll'
          A first chance exception of type 'System.ObjectD isposedExceptio n' occurred in System.dll
          A first chance exception of type 'System.Net.Web Exception' occurred in System.dll
          An unhandled exception of type 'System.Net.Web Exception' occurred in System.dll

          Additional information: The server committed a protocol violation. Section=Respons eStatusLine

          The thread 0x1210 has exited with code 0 (0x0).
          The program '[4712] WindowsFormsApp lication1.vshos t.exe: Managed' has exited with code -532459699 (0xe0434f4d).

          ----------------------------------------------------------------------------

          reply awaited.

          Thanks
          Last edited by Curtis Rutland; Jul 7 '08, 08:41 PM. Reason: Added Code Tags - Please use the # button

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Looks like the results from the server don't come back in the http/1.1 standard.
            (Possibly doesn't return ANY status line is my guess)

            Comment

            Working...