469,904 Members | 2,123 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

System.Net.WebException

sid0404
16
Hi

I am new to the programming in c#, I have an application where I need to get data from a website now I am getting an error message if I give the input as two strings, say Sun Microsystems while f I just give one input then I have the following error message:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

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

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

The code is:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. using System.Data;
  6. using System.IO;
  7. using System.Net;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11.  
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.  
  16.     public partial class Form2 : Form
  17.     {
  18.  
  19.         List<string> patent_numbers = new List<string>();
  20.         List<string> assignee_list = new List<string>();
  21.  
  22.         public string query_name;
  23.  
  24.         public Form2(string t)
  25.         {
  26.             InitializeComponent();
  27.  
  28.             query_name = t;
  29.             string url_to_append = "http://patft.uspto.gov";
  30.             string webpage_data = "&TERM1=" + t + "&FIELD1=ASNM&co1=AND&TERM2=&FIELD2=&d=PTXT";
  31.             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";
  32.             ASCIIEncoding encoding = new ASCIIEncoding();
  33.             byte[] data = encoding.GetBytes(webpage_data);
  34.             webpage = webpage + "?" + webpage_data;
  35.             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(webpage);
  36.             myRequest.Credentials = CredentialCache.DefaultCredentials;
  37.             myRequest.Method = "GET";
  38.  
  39.             HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
  40.             Stream receive_stream = response.GetResponseStream();
  41.             StreamReader read_stream = new StreamReader(receive_stream, Encoding.UTF8);
  42.             string str = read_stream.ReadToEnd();
  43.  
  44.             response.Close();
  45.             read_stream.Close();
  46.  
  47.             int total_patents = total_number_of_patents(str);
  48.  
  49.             label1.Text = t + " Total number of Patents: " + total_patents;
  50.  
  51.             int first_entry = str.IndexOf(">1<");
  52.             int index_val_href_start = str.IndexOf("HREF=", first_entry);
  53.             int index_val_href_url_end = str.IndexOf(">", index_val_href_start);
  54.             string str_test = str.Substring(index_val_href_start + 5, index_val_href_url_end - index_val_href_start - 5);
  55.             string sample_url = url_to_append + str_test;
  56.  
  57.             // this is to break url into 2 parts as each page has a different url for the values.
  58.  
  59.             int length_string = sample_url.Length;
  60.             int index_start = sample_url.IndexOf("&r=");
  61.             string url_1 = sample_url.Substring(0, index_start + 3);
  62.             string url_2 = sample_url.Substring(index_start + 4, length_string - index_start - 4);
  63.  
  64.             // this will iterate the loop to the total number of the patents
  65.  
  66.             richTextBox1.Text = "s/no      PATENT NUMBER                         ASSIGNEE NAME\n\n";
  67.  
  68.             for (int i = 1; i <= total_patents; i++)
  69.             {
  70.                 string final_url = url_1 + i + url_2;
  71.                 final_url = final_url.Replace("%3F", "");
  72.                 string[] truqe = new string[2];
  73.                 truqe = find_assignee_name_and_patent_number(final_url);
  74.  
  75.                 truqe[0] = truqe[0].Replace(',', ' ');
  76.                 patent_numbers.Add(truqe[1]);
  77.                 assignee_list.Add(truqe[0]);
  78.  
  79.                 richTextBox1.AppendText(" " + i + "               " + truqe[1] + "           ::            " + truqe[0]);
  80.                 richTextBox1.AppendText("\n");
  81.  
  82.             }
  83.  
  84.  
  85.         }
  86.  
  87.  
  88.         /*
  89.          * this is used to find the total number of the patents
  90.          * for any given string.
  91.          */
  92.  
  93.         private int total_number_of_patents(string html_string)
  94.         {
  95.             int index1 = html_string.IndexOf("DOCS:");
  96.             int index2 = html_string.IndexOf(">", index1 + 2);
  97.  
  98.             string str_return = html_string.Substring(index1 + 6, index2 - index1 - 6);
  99.  
  100.             int patents = int.Parse(str_return);
  101.  
  102.             return patents;
  103.         }
  104.  
  105.         /*
  106.          * this method is use to get the
  107.          * value of the assignee name and the
  108.          * corresponding patnet number of it
  109.          * from the HTML source code it parses
  110.          * the values to get them.
  111.          */
  112.  
  113.         private string[] find_assignee_name_and_patent_number(string url)
  114.         {
  115.             string[] str = new string[2];
  116.  
  117.             ASCIIEncoding encoding = new ASCIIEncoding();
  118.             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  119.             myRequest.Credentials = CredentialCache.DefaultCredentials;
  120.             HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
  121.             Stream receive_stream = response.GetResponseStream();
  122.             StreamReader read_stream = new StreamReader(receive_stream, Encoding.UTF8);
  123.  
  124.             string str_return = read_stream.ReadToEnd();
  125.             response.Close();
  126.             read_stream.Close();
  127.  
  128.             int index_1 = str_return.IndexOf("Assignee:");
  129.             int index_2 = str_return.IndexOf("#h2", index_1);
  130.             index_1 = str_return.IndexOf("<BR>", index_2);
  131.             str[0] = str_return.Substring(index_2 + 4, index_1 - index_2);
  132.  
  133.             str[0] = str[0].Replace("\n", "");
  134.             str[0] = str[0].Replace("</A>", "");
  135.             str[0] = str[0].Replace("<B>", "");
  136.             str[0] = str[0].Replace("<BR>", "");
  137.             str[0] = str[0].Replace("</I>", "");
  138.             str[0] = str[0].Replace("<I>", "");
  139.             str[0] = str[0].Replace("</B>", "");
  140.  
  141.             int index_3 = str_return.IndexOf("Patent: ");
  142.             int index_4 = str_return.IndexOf("<", index_3);
  143.  
  144.             str[1] = str_return.Substring(index_3 + 7, index_4 - index_3 - 7);
  145.  
  146.             return str;
  147.         }
  148.  
  149.         private void label1_Click(object sender, EventArgs e)
  150.         {
  151.  
  152.         }
  153.  
  154.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  155.         {
  156.  
  157.         }
  158.  
  159.         private void Form2_Load(object sender, EventArgs e)
  160.         {
  161.  
  162.         }
  163.  
  164.         private void Save_button_Click(object sender, EventArgs e)
  165.         {
  166.             SaveFileDialog sv_dialog = new SaveFileDialog();
  167.  
  168.             // set the properties of the Save As File Dialog
  169.  
  170.             sv_dialog.DefaultExt = ".csv";
  171.             sv_dialog.Filter = "Comma Seperated Values(*.csv) | *.csv | Text File(*.txt) | *.txt | Microsoft Word Document(*.doc) | *.doc";
  172.             sv_dialog.FilterIndex = 1;
  173.             sv_dialog.OverwritePrompt = true;
  174.             sv_dialog.Title = "Save As . . . !!";
  175.             sv_dialog.FileName = query_name;
  176.  
  177.  
  178.             // writes the data to the loaction specified by the user
  179.  
  180.             if (sv_dialog.ShowDialog() == DialogResult.OK)
  181.             {
  182.  
  183.                 TextWriter tw = new StreamWriter(sv_dialog.FileName, true);
  184.  
  185.                 for (int i = 0; i < assignee_list.Count; i++)
  186.                 {
  187.                     tw.Write(patent_numbers.ElementAt(i) + ",");
  188.                     tw.WriteLine(assignee_list.ElementAt(i));
  189.                 }
  190.  
  191.                 tw.Close();
  192.  
  193.             }
  194.  
  195.  
  196.         }
  197.  
  198.         private void Exit_Click(object sender, EventArgs e)
  199.         {
  200.             Application.Exit();
  201.         }
  202.  
  203.         private void append_btn_Click(object sender, EventArgs e)
  204.         {
  205.  
  206.             OpenFileDialog op_dialog = new OpenFileDialog();
  207.             op_dialog.DefaultExt = ".*";
  208.             op_dialog.Filter = "Comma Seperated Values(*.csv) | *.csv | Text File(*.txt) | *.txt | Microsoft Word Document(*.doc) | *.doc | All Files(*.*) | *.*";
  209.  
  210.             op_dialog.Title = "Select the file to append";
  211.  
  212.             if (op_dialog.ShowDialog() == DialogResult.Cancel)
  213.                 return;
  214.  
  215.             try
  216.             {
  217.                 StreamWriter sw;
  218.                 sw = File.AppendText(op_dialog.FileName);
  219.  
  220.  
  221.                 for (int i = 0; i < assignee_list.Count; i++)
  222.                 {
  223.                     String test = patent_numbers.ElementAt(i) + "," + assignee_list.ElementAt(i);
  224.                     sw.WriteLine(test);
  225.                 }
  226.  
  227.                 sw.Flush();
  228.                 sw.Close();
  229.  
  230.             }
  231.             catch (Exception ex)
  232.             {
  233.                 MessageBox.Show("Error in appending text to the file" + ex.Message);
  234.             }
  235.         }
  236.     }
  237. }
----------------------------------------------------------------------------------------------------------------------------------------

the line in bold red is responsible for the error message:

I request you to let me know what is wrong and whay am I getting the weird erro message when I give input as Sun Microsystems which consists of 2 words.

I do not understand why am I getting this exception and what is the solution to this ?
Clarification appreciated.
Aug 1 '08 #1
3 1554
Curtis Rutland
3,256 Expert 2GB
Please remember to use CODE tags when you post (the # button in the text editor). It makes blocks of code much more readable. Please read the Posting Guidelines.

MODERATOR

Also, you will have to tell us which line the error happened on, since there were no bold and red lines, and even if there were, they won't show up when surrounded by code tags.
Aug 1 '08 #2
sid0404
16
I am talking about the line 120 in bold
Aug 1 '08 #3
Curtis Rutland
3,256 Expert 2GB
OK, first, I had already fixed the problem for you, so your extra post was needless. I also removed the quote from your last post, because it was also long and unnecessary.

As I said before, bold lines don't show up in code. So just look at your first post, and tell us which line it is that throws the error.
Aug 1 '08 #4

Post your reply

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

Similar topics

2 posts views Thread by genc_ ymeri at hotmail dot com | last post: by
reply views Thread by Kumar | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.