Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Using System. Net;
Using System. Threading;
Namespace WindowsFormsApplication1
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
Thread oThread = new Thread (new ThreadStart (Gethttp ));
OThread. Start ();
}
Private void Gethttp ()
{
WebRequest request = WebRequest. Create ("http://api.bing.net/xml.aspx? AppId = 434097AB350E29EED406F35E8364181516BCA5FC & Query = ip: "+ textBox1.Text +" & Verstion = 2.0 & Market = zh-CN & Sources = web + spell & web. count = 50 & web. offset = 0 "); // WebRequest. the Create method returns the WebRequest subclass HttpWebRequest.
WebResponse response = request. GetResponse (); // WebRequest. GetResponse method, returns the response to the Internet request
Stream resStream = response. GetResponseStream (); // The WebResponse. GetResponseStream method returns data streams from Internet resources.
Encoding enc = Encoding. GetEncoding ("UTF-8"); // if it is garbled, change it to UTF-8/GB2312
StreamReader sr = new StreamReader (resStream, enc); // namespace: System. IO. The StreamReader class implements a TextReader (TextReader class, indicating that the reader can read the continuous character series) so that it can read characters from the byte stream with a specific encoding.
TextBox2.Text = sr. ReadToEnd ();
ResStream. Close ();
Sr. Close ();
}
}
}
Error after running
The Inter-thread operation is invalid: It is accessed by a thread that does not create the control "textBox2.
There are two solutions, but the second method is recommended: www.2cto.com
1,
Control. checkforillegalcrossthreadcils = false;
When the thread starts, add this sentence. OK. No error is found ~
2,
Because this control is created by the main thread, it is attached to the main thread that creates the form. therefore, to access the resource type in the subthread, we need to call it across threads. textBox2.InvokeRequire is used to determine whether cross-thread is required. If True is returned, cross-thread is required. At this time, you need to define a delegate class to encapsulate a method containing a value assignment statement and call this delegate through textBox2.Invoke. the Code is as follows:
Public delegate void SetTextHandler (string text );
Private void SetText (string text)
{
If (textBox4.InvokeRequired = true)
{
SetTextHandler set = new SetTextHandler (SetText); // the delegate's method parameters should be consistent with SetText
TextBox2.Invoke (set, new object [] {text}); // The second parameter of this method is used to pass in the method, instead of text
}
Else
{
TextBox2.Text = text;
}
}
The Gethttp function can be rewritten:
WebRequest request = WebRequest. Create (textBox1.Text); // WebRequest. Create method, return the subclass HttpWebRequest of WebRequest
WebResponse response = request. GetResponse (); // WebRequest. GetResponse method, returns the response to the Internet request
Stream resStream = response. GetResponseStream (); // The WebResponse. GetResponseStream method returns data streams from Internet resources.
Encoding enc = Encoding. GetEncoding ("UTF-8"); // if it is garbled, change it to UTF-8/GB2312
StreamReader sr = new StreamReader (resStream, enc); // namespace: System. IO. The StreamReader class implements a TextReader (TextReader class, indicating that the reader can read the continuous character series) so that it can read characters from the byte stream with a specific encoding.
SetText (sr. ReadToEnd (). ToString ());
ResStream. Close ();
Sr. Close ();
Note that the SetText (sr. ReadToEnd (). ToString () is the main point to be modified.
From Shine's holy heaven-Min Chen 〃