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
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.IO;
- using System.Net;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- namespace WindowsFormsApplication1
- {
- public partial class Form2 : Form
- {
- List<string> patent_numbers = new List<string>();
- List<string> assignee_list = new List<string>();
- public string query_name;
- public Form2(string t)
- {
- InitializeComponent();
- query_name = t;
- string url_to_append = "http://patft.uspto.gov";
- string webpage_data = "&TERM1=" + t + "&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";
- 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 = "GET";
- HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
- Stream receive_stream = response.GetResponseStream();
- StreamReader read_stream = new StreamReader(receive_stream, Encoding.UTF8);
- string str = read_stream.ReadToEnd();
- response.Close();
- read_stream.Close();
- int total_patents = total_number_of_patents(str);
- label1.Text = t + " Total number of Patents: " + total_patents;
- int first_entry = str.IndexOf(">1<");
- int index_val_href_start = str.IndexOf("HREF=", first_entry);
- int index_val_href_url_end = str.IndexOf(">", index_val_href_start);
- string str_test = str.Substring(index_val_href_start + 5, index_val_href_url_end - index_val_href_start - 5);
- string sample_url = url_to_append + str_test;
- // this is to break url into 2 parts as each page has a different url for the values.
- int length_string = sample_url.Length;
- int index_start = sample_url.IndexOf("&r=");
- string url_1 = sample_url.Substring(0, index_start + 3);
- string url_2 = sample_url.Substring(index_start + 4, length_string - index_start - 4);
- // this will iterate the loop to the total number of the patents
- richTextBox1.Text = "s/no PATENT NUMBER ASSIGNEE NAME\n\n";
- for (int i = 1; i <= total_patents; i++)
- {
- string final_url = url_1 + i + url_2;
- final_url = final_url.Replace("%3F", "");
- string[] truqe = new string[2];
- truqe = find_assignee_name_and_patent_number(final_url);
- truqe[0] = truqe[0].Replace(',', ' ');
- patent_numbers.Add(truqe[1]);
- assignee_list.Add(truqe[0]);
- richTextBox1.AppendText(" " + i + " " + truqe[1] + " :: " + truqe[0]);
- richTextBox1.AppendText("\n");
- }
- }
- /*
- * this is used to find the total number of the patents
- * for any given string.
- */
- private int total_number_of_patents(string html_string)
- {
- int index1 = html_string.IndexOf("DOCS:");
- int index2 = html_string.IndexOf(">", index1 + 2);
- string str_return = html_string.Substring(index1 + 6, index2 - index1 - 6);
- int patents = int.Parse(str_return);
- return patents;
- }
- /*
- * this method is use to get the
- * value of the assignee name and the
- * corresponding patnet number of it
- * from the HTML source code it parses
- * the values to get them.
- */
- private string[] find_assignee_name_and_patent_number(string url)
- {
- string[] str = new string[2];
- ASCIIEncoding encoding = new ASCIIEncoding();
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
- myRequest.Credentials = CredentialCache.DefaultCredentials;
- HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
- Stream receive_stream = response.GetResponseStream();
- StreamReader read_stream = new StreamReader(receive_stream, Encoding.UTF8);
- string str_return = read_stream.ReadToEnd();
- response.Close();
- read_stream.Close();
- int index_1 = str_return.IndexOf("Assignee:");
- int index_2 = str_return.IndexOf("#h2", index_1);
- index_1 = str_return.IndexOf("<BR>", index_2);
- str[0] = str_return.Substring(index_2 + 4, index_1 - index_2);
- str[0] = str[0].Replace("\n", "");
- str[0] = str[0].Replace("</A>", "");
- str[0] = str[0].Replace("<B>", "");
- str[0] = str[0].Replace("<BR>", "");
- str[0] = str[0].Replace("</I>", "");
- str[0] = str[0].Replace("<I>", "");
- str[0] = str[0].Replace("</B>", "");
- int index_3 = str_return.IndexOf("Patent: ");
- int index_4 = str_return.IndexOf("<", index_3);
- str[1] = str_return.Substring(index_3 + 7, index_4 - index_3 - 7);
- return str;
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void Form2_Load(object sender, EventArgs e)
- {
- }
- private void Save_button_Click(object sender, EventArgs e)
- {
- SaveFileDialog sv_dialog = new SaveFileDialog();
- // set the properties of the Save As File Dialog
- sv_dialog.DefaultExt = ".csv";
- sv_dialog.Filter = "Comma Seperated Values(*.csv) | *.csv | Text File(*.txt) | *.txt | Microsoft Word Document(*.doc) | *.doc";
- sv_dialog.FilterIndex = 1;
- sv_dialog.OverwritePrompt = true;
- sv_dialog.Title = "Save As . . . !!";
- sv_dialog.FileName = query_name;
- // writes the data to the loaction specified by the user
- if (sv_dialog.ShowDialog() == DialogResult.OK)
- {
- TextWriter tw = new StreamWriter(sv_dialog.FileName, true);
- for (int i = 0; i < assignee_list.Count; i++)
- {
- tw.Write(patent_numbers.ElementAt(i) + ",");
- tw.WriteLine(assignee_list.ElementAt(i));
- }
- tw.Close();
- }
- }
- private void Exit_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void append_btn_Click(object sender, EventArgs e)
- {
- OpenFileDialog op_dialog = new OpenFileDialog();
- op_dialog.DefaultExt = ".*";
- op_dialog.Filter = "Comma Seperated Values(*.csv) | *.csv | Text File(*.txt) | *.txt | Microsoft Word Document(*.doc) | *.doc | All Files(*.*) | *.*";
- op_dialog.Title = "Select the file to append";
- if (op_dialog.ShowDialog() == DialogResult.Cancel)
- return;
- try
- {
- StreamWriter sw;
- sw = File.AppendText(op_dialog.FileName);
- for (int i = 0; i < assignee_list.Count; i++)
- {
- String test = patent_numbers.ElementAt(i) + "," + assignee_list.ElementAt(i);
- sw.WriteLine(test);
- }
- sw.Flush();
- sw.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error in appending text to the file" + ex.Message);
- }
- }
- }
- }
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.