C # Winfrom Little Yellow Chicken function call,

Source: Internet
Author: User

C # Winfrom Little Yellow Chicken function call,

Recently, a public platform has been studied to build a chatbot that calls the public interface of xiaohuang chicken to chat online with xiaohuang chicken.

Interface calling is not very troublesome, but php version, so I studied the functional modules of C,

Winfrom

The background interface code is

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 using System. threading; 10 11 namespace small base chicken 12 {13 public partial class Form1: Form 14 {15 private static string cookie = null; 16 private string msg = ""; 17 private Thread Th1 = null; 18 private Thread th3 = null; 19 private bool changed = false; 20 21 public Form1 () 22 {23 InitializeComponent (); 24 simsimi. setAllowUnsafeHeaderParsing20 (); 25 Thread th = new Thread (new ThreadStart (Cookie_Thread); 26 Th1 = new Thread (new ThreadStart (Hi_Thread )); 27 th3 = new Thread (new ThreadStart (PowerOn_Thread); 28 th. start (); 29 th2.Start (); 30 th3.Start (); 31} 32 33 private void button#click (obje Ct sender, EventArgs e) 34 {35 if (textBox1.Text! = "") 36 {37 changed = true; 38 msg = textBox1.Text; 39 textBox1.Text = ""; 40 richTextBox1.Text + = String. format ("Me: {0} \ n", msg); 41 changed = false; 42} 43} 44 45 private static void Cookie_Thread () 46 {47 cookie = simsimi. getCookie (); 48} 49 private void PowerOn_Thread () 50 {51 while (cookie = null) 52 {53 this. invoke (new Action (Update_PowerText), null); 54 Thread. sleep (1500); 55} 56 t His. Invoke (new Action (Update_PowerFinalText), null); 57} 58 private void Update_PowerText () 59 {60 richTextBox1.Text + = "is running... \ N "; 61} 62 private void Update_PowerFinalText () 63 {64 richTextBox1.Text + =" Well, the chicken is finally opened. \ N chicken: Hello, I'm a chicken. O (partition _ threads) o \ n "; 65} 66 private void Hi_Thread () 67 {68 while (true) 69 {70 while (changed) 71 {72 this. invoke (new Action (Update_Text), null); 73 break; 74} 75} 76} 77 private void Update_Text () 78 {79 richTextBox1.Text + = String. format ("chicken: {0} \ n", simsimi. hi_Simsimi (msg, cookie); 80} 81 82 private void textBox1_KeyUp (object sender, KeyEventArgs e) 83 {84 if (e. keyValue = 13) 85 {86 if (tex TBox1.Text! = "") 87 {88 changed = true; 89 msg = textBox1.Text; 90 textBox1.Text = ""; 91 richTextBox1.Text + = String. format ("Me: {0} \ n", msg); 92 changed = false; 93} 94} 95} 96 97 private void richTextBox1_TextChanged (object sender, EventArgs e) 98 {99 // set the cursor position to 100 this. richTextBox1.SelectionStart = richTextBox1.TextLength-1; 101 // scroll to the current cursor 102 this. richTextBox1.ScrollToCaret (); 103} 104 105} 106}View Code

Application class code: the application interface uses a json-format content that is passed back from xiaohuang chicken. Therefore, I am applying Newtonsoft, a common method on the Internet, to solve the json parsing problem. json. You can download the corresponding DLL from the Internet.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. net; 6 using System. IO; 7 using System. IO. compression; 8 using System. reflection; 9 using Newtonsoft. json; 10 using Newtonsoft. json. linq; 11 12 13 namespace chicken 14 {15 class simsimi 16 {17 // <summary> 18 // Cookie 19 /// </summary> 20 // <returns> </returns> 21 public static string GetCookie () 22 {23 string Cookiesstr = null; 24 CookieCollection cookies = new CookieCollection (); 25 HttpWebRequest request = null; 26 request = (HttpWebRequest) WebRequest. create (" http://www.simsimi.com/talk.htm "); 27 request. cookieContainer = new CookieContainer (); 28 request. cookieContainer. add (cookies); 29 // Get the response from the server and save the cookies from the request .. 30 HttpWebResponse response = (HttpWebResponse) request. getResponse (); 31 Cookiesstr = request. cookieContainer. getCookieHeader (request. requestUri); 32 33 return Cookiesstr; 34} 35 36 37 public static string Hi_Simsimi (string que, string cookies) 38 {39 string ans = "let's change the topic "; 40 string url = String. format (" http://www.simsimi.com/func/reqN?lc=ch&ft=0.0&req= %0}&fl;http%3a%2f%2fwww.simsimi.com%2ftalk.htm ", que); 41 HttpWebRequest hi_request = null; 42 try 43 {44 hi_request = (HttpWebRequest) WebRequest. create (url); 45 hi_request.Method = "GET"; 46 hi_request.KeepAlive = true; 47 hi_request.ServicePoint.Expect100Continue = false; 48 49 hi_request.AllowWriteStreamBuffering = false; 50 // terminal information 51 hi_request.Accept = "application/json, text/javascript, */*; q = 0.01"; 52 hi_request.Headers.Add ("Accept-Language ", "zh-cn"); 53 hi_request.Headers.Add ("Accept-Encoding", "gzip, deflate"); 54 hi_request.Headers.Add ("Cookie", cookies + "; simsimi_uid = 1; "); 55 hi_request.Referer =" http://www.simsimi.com/talk.htm "; 56 hi_request.UserAgent =" Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2 ;. net clr 2.0.50727 ;. net clr 3.5.30729 ;. net clr 3.0.30729 ;. NET4.0C ;. NET4.0E) "; 57 hi_request.ContentType =" application/json; charset = UTF-8 "; 58 59 hi_request.AllowAutoRedirect = false; 60 HttpWebResponse hi_response = (HttpWebResponse) hi_request.GetResponse (); 61 StreamReader sr = new StreamReader (hi_respo Neuron. GetResponseStream (), Encoding. UTF8); 62 ans = sr. ReadToEnd (); 63 if (ans! = "{}") 64 {65 66 JObject jo = JObject. parse (ans); 67 string [] ArrayList = jo. properties (). select (item => item. value. toString ()). toArray (); 68 if (ArrayList [0] = "200") 69 {70 ans = ArrayList [6]; 71} 72 else 73 {74 ans = "not understood, can I say it again? "; 75} 76} 77 hi_request.Abort (); 78 hi_response.Close (); 79} 80 catch (System. exception e) 81 {82 83 Console. writeLine (e. toString (); 84 // return e. toString (); 85 return "Sorry, it's too bad ~ "; 86} 87 88 return ans; 89} 90 91 92 93 public static bool SetAllowUnsafeHeaderParsing20 () 94 {95 // Get the assembly that contains the internal class 96 Assembly aNetAssembly = Assembly. getAssembly (typeof (System. net. configuration. settingsSection); 97 if (aNetAssembly! = Null) 98 {99 // Use the assembly in order to get the internal type for the internal class100type aSettingsType = aNetAssembly. getType ("System. net. configuration. settingsSectionInternal "); 101 if (aSettingsType! = Null) 102 {103 // Use the internal static property to get an instance of the internal settings class.104 // If the static instance isn't created allready the property will create it for us.105 object anInstance = aSettingsType. invokeMember ("Section", 106 BindingFlags. static | BindingFlags. getProperty | BindingFlags. nonPublic, null, null, new object [] {}); 107 108 if (anInstance! = Null) 109 {110 // Locate the private bool field that tells the framework is unsafe header parsing shoshould be allowed or not111 FieldInfo aUseUnsafeHeaderParsing = aSettingsType. getField ("useUnsafeHeaderParsing", BindingFlags. nonPublic | BindingFlags. instance); 112 if (aUseUnsafeHeaderParsing! = Null) 113 {114 aUseUnsafeHeaderParsing. SetValue (anInstance, true); 115 return true; 116} 117} 118} 119 return false; 120} 121} 122}View Code

 


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.