By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

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

sid0404
P: 16
Hi

I am new to the visualstudio.net

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:
------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. public Form2(TextBox t)
  2.         {
  3.             InitializeComponent();
  4.  
  5.             label1.Text = t.Text;
  6.  
  7.             StringBuilder sb = new StringBuilder();
  8.             byte[] buf = new byte[100000000];
  9.  
  10.  
  11.  
  12.             try
  13.             {
  14.                 string webpage_data = t.Text + "&FIELD1=ASNM";
  15.                 //            string webpage_data = "TERM1=" + t.Text + "&FIELD1=ASNM";
  16. //                string webpage = "http://patft.uspto.gov/netacgi/nph-Parser";
  17.                    string webpage = "http://patft.uspto.gov/netahtml/PTO/search-bool.html";
  18.  
  19.                 ASCIIEncoding encoding = new ASCIIEncoding();
  20.                 byte[] data = encoding.GetBytes(webpage_data);
  21. //                webpage = webpage + "?" + webpage_data;
  22.                 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webpage);
  23.  
  24.                 myRequest.Credentials = CredentialCache.DefaultCredentials;
  25.  
  26.  
  27.                 myRequest.ContentLength = data.Length;
  28.                 myRequest.Method = "GET";
  29.                 myRequest.ContentType = "application/x-www-form-urlencoded";
  30.                 myRequest.ContentLength = data.Length;  
  31.  
  32.                 Stream newStream = myRequest.GetRequestStream();
  33.  
  34.                 newStream.Write(data, 0, data.Length);
  35.  
  36.                 HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
  37.                 Stream receive_stream = response.GetResponseStream();
  38.  
  39.                 StreamReader read_stream = new StreamReader(receive_stream, Encoding.UTF8);
  40.  
  41.                 string str = read_stream.ReadToEnd();
  42.  
  43.                 newStream.Close();
  44.  
  45.  
  46.                 //            response.GetResponseStream(read_stream.ReadToEnd());
  47.  
  48.                 response.Close();
  49.                 read_stream.Close();
  50.  
  51.                 richTextBox1.Text = str;
  52.  
  53.             }
  54.             catch (Exception)
  55.             {
  56.  
  57.                 throw;
  58.             }
  59.         }
  60.  
---------------------------------------------------------------------------------------------------------------------------------------------------

and the log at the output tab is as follows:

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

'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a 5c561934e089\mscorlib.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStud io.HostingProcess.Utilities\9.0.0.0__b03f5f7f11d50 a3a\Microsoft.VisualStudio.HostingProcess.Utilitie s.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Windows.Forms \2.0.0.0__b77a5c561934e089\System.Windows.Forms.dl l'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a 5c561934e089\System.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0 .0__b03f5f7f11d50a3a\System.Drawing.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStud io.HostingProcess.Utilities.Sync\9.0.0.0__b03f5f7f 11d50a3a\Microsoft.VisualStudio.HostingProcess.Uti lities.Sync.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStud io.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Micr osoft.VisualStudio.Debugger.Runtime.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Users\Siddharth\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsForm sApplication1\bin\Debug\WindowsFormsApplication1.v shost.exe'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0_ _b77a5c561934e089\System.Core.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5. 0.0__b77a5c561934e089\System.Xml.Linq.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Data.DataSetE xtensions\3.5.0.0__b77a5c561934e089\System.Data.Da taSetExtensions.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b 77a5c561934e089\System.Data.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Deployment\2. 0.0.0__b03f5f7f11d50a3a\System.Deployment.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__ b77a5c561934e089\System.Xml.dll'
The thread 0x11f0 has exited with code 0 (0x0).
The thread 0x129c has exited with code 0 (0x0).
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Users\Siddharth\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsForm sApplication1\bin\Debug\WindowsFormsApplication1.e xe', Symbols loaded.
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration \2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dl l'
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.dll
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in WindowsFormsApplication1.exe
An unhandled exception of type 'System.Net.ProtocolViolationException' occurred in WindowsFormsApplication1.exe

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

The program '[3816] WindowsFormsApplication1.vshost.exe: Managed' has exited with code -805306369 (0xcfffffff).
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Thanks

Reply awaited

Sid
Jul 7 '08 #1
Share this Question
Share on Google+
5 Replies


Plater
Expert 5K+
P: 7,872
Well, as the error message says, if you are using the GET method, there is no content data.
Perhaps you wanted the POST method?
Jul 7 '08 #2

sid0404
P: 16
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.WebException at the line

HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();

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 ?
Jul 7 '08 #3

Plater
Expert 5K+
P: 7,872
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.
Jul 7 '08 #4

sid0404
P: 16
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.webexception, the code is

----------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1.     using System;
  2.     using System.Collections.Generic;
  3.     using System.ComponentModel;
  4.     using System.Data;
  5.     using System.IO;
  6. using System.Net;
  7.     using System.Drawing;
  8.     using System.Linq;
  9.     using System.Text;
  10.     using System.Windows.Forms;
  11.  
  12.     namespace WindowsFormsApplication1
  13.     {
  14.         public partial class Form2 : Form
  15.         {
  16.             public Form2(TextBox t)
  17.             {
  18.                 InitializeComponent();
  19.  
  20.                 label1.Text = t.Text;
  21.  
  22.                 StringBuilder sb = new StringBuilder();
  23.                 byte[] buf = new byte[100000000];
  24.  
  25.                 string webpage_data = "&TERM1=google&FIELD1=ASNM&co1=AND&TERM2=&FIELD2=&d=PTXT";
  26.                 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";
  27.           //             string webpage = "http://patft.uspto.gov/netahtml/PTO/search-bool.html";
  28.  
  29.                     ASCIIEncoding encoding = new ASCIIEncoding();
  30.                     byte[] data = encoding.GetBytes(webpage_data);
  31.                     webpage = webpage + "?"+ webpage_data;
  32.                     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webpage);
  33.  
  34.                     myRequest.Credentials = CredentialCache.DefaultCredentials;
  35.                     myRequest.Method = "POST";
  36.  
  37.     /*            
  38.                     myRequest.ContentLength = data.Length;
  39.                     myRequest.Method = "POST";
  40.                     myRequest.ContentType = "application/x-www-form-urlencoded";
  41.                     myRequest.ContentLength = data.Length;   
  42.  
  43.                     Stream newStream = myRequest.GetRequestStream();
  44.  
  45.                     newStream.Write(data, 0, data.Length);
  46. */                
  47.                     HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
  48.                     Stream receive_stream = response.GetResponseStream();
  49.  
  50.                     StreamReader read_stream = new StreamReader(receive_stream, Encoding.UTF8);
  51.  
  52.                     string str = read_stream.ReadToEnd();
  53.  
  54.         //            newStream.Close();
  55.  
  56.  
  57.                     //            response.GetResponseStream(read_stream.ReadToEnd());
  58.  
  59.                     response.Close();
  60.                     read_stream.Close();
  61.  
  62.                     richTextBox1.Text = str;
  63.  
  64.     //            }
  65.             }
  66.  
  67.             private void label1_Click(object sender, EventArgs e)
  68.             {
  69.  
  70.             }
  71.  
  72.             private void richTextBox1_TextChanged(object sender, EventArgs e)
  73.             {
  74.  
  75.             }
  76.         }
  77.     }
  78.  
----------------------------------------------------------------------------
while the log on the output tab looks like this :
----------------------------------------------------------------------------
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a 5c561934e089\mscorlib.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStud io.HostingProcess.Utilities\9.0.0.0__b03f5f7f11d50 a3a\Microsoft.VisualStudio.HostingProcess.Utilitie s.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Windows.Forms \2.0.0.0__b77a5c561934e089\System.Windows.Forms.dl l'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System\2.0.0.0__b77a 5c561934e089\System.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Drawing\2.0.0 .0__b03f5f7f11d50a3a\System.Drawing.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStud io.HostingProcess.Utilities.Sync\9.0.0.0__b03f5f7f 11d50a3a\Microsoft.VisualStudio.HostingProcess.Uti lities.Sync.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStud io.Debugger.Runtime\9.0.0.0__b03f5f7f11d50a3a\Micr osoft.VisualStudio.Debugger.Runtime.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Users\Siddharth\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsForm sApplication1\bin\Debug\WindowsFormsApplication1.v shost.exe'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0_ _b77a5c561934e089\System.Core.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml.Linq\3.5. 0.0__b77a5c561934e089\System.Xml.Linq.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Data.DataSetE xtensions\3.5.0.0__b77a5c561934e089\System.Data.Da taSetExtensions.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_32\System.Data\2.0.0.0__b 77a5c561934e089\System.Data.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Deployment\2. 0.0.0__b03f5f7f11d50a3a\System.Deployment.dll'
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Xml\2.0.0.0__ b77a5c561934e089\System.Xml.dll'
The thread 0x9d4 has exited with code 0 (0x0).
The thread 0x10b8 has exited with code 0 (0x0).
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Users\Siddharth\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsForm sApplication1\bin\Debug\WindowsFormsApplication1.e xe', Symbols loaded.
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
'WindowsFormsApplication1.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration \2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dl l'
A first chance exception of type 'System.ObjectDisposedException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.dll
An unhandled exception of type 'System.Net.WebException' occurred in System.dll

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

The thread 0x1210 has exited with code 0 (0x0).
The program '[4712] WindowsFormsApplication1.vshost.exe: Managed' has exited with code -532459699 (0xe0434f4d).

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

reply awaited.

Thanks
Jul 7 '08 #5

Plater
Expert 5K+
P: 7,872
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)
Jul 7 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.