473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1882
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

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

Similar topics

0
by: Joe Bloggs | last post by:
I have a C# MRS application that uses the ReportingService's Render method to retrieve a byte array containing a report. The following error message occurs An unhandled exception of type...
2
by: genc_ ymeri at hotmail dot com | last post by:
Hi, I'm trying to recieve the response status code rather than the message in the below code : try { responseArray = myWebClient.UploadValues(uriString,"POST",myNameValueCollection); }...
0
by: yariv | last post by:
Hello All, I am having a very strange problem. while trying to access http page on the web. I happen to have some problems at specific machines. the exception I get is: *************...
0
by: Kumar | last post by:
Hi all, I have the following code which uses WebClient.UploadValues myNameValueCollection.Add("Name", name) myNameValueCollection.Add("Age", age) .............. ............. Dim web As New...
1
by: etantonio | last post by:
Good morning, I've a problem, in the past I translate my site from google or altavista with a code similar to this : <%@ Page Language="c#" Trace="true" Debug="true" %> <%@ import...
1
by: Albert Ludwig | last post by:
Hallo NewsGroup, in meiner Anwendung rufe ich ein Internet-asp-script auf. Diese Funktionalität habe ich in der Funktion "fuwHandelWebRequestPost" (siehe unten) gekapselt. Diese Funktion...
2
by: tlan | last post by:
Hi, I got this error when I move my web service to Windows2003 server. I spent hours scouting on the internet and could find any answer. Please help!!! I the webservice is timeout between 1...
1
by: Tim Reynolds | last post by:
Team, From a windows service, we consume a web service on another server and occasionally receive System.Net.WebException: The underlying connection was closed:. For some clients we call, we do...
8
by: Tim Reynolds | last post by:
Our .Net application calls a web method of aplpication 2 that resides on their Apache server. When I as a developer C#, Studios 2003, make the call to their web method from my desktop, I receive no...
2
by: Scott McFadden | last post by:
When I invoke two web service methods sequentially with no delay, the first web method invocation goes smooth while the 2nd one generates the dredded: System.Net.WebException: The underlying...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.