Originally never carefully studied the HTTP protocol, today because of the company's business needs, debugging a half-day, and finally now will WinForm with the HTTP protocol and service-side communication, which is commonly used in the post and get mode;
Look closely at Renren and Sina, and so on most of the use get way to obtain data, MSN as follows:
Or do not leave the main purpose of this article:
The simulation implementation login code is as follows:
1 Private voidPicturebox3_click (Objectsender, EventArgs e)2 {3 stringstrUserName = TextEdit1.Text.Trim ();//User name4 stringStruserpwd = TextEdit2.Text.Trim ();//Password5 6 if(string. IsNullOrEmpty (strUserName) | |string. IsNullOrEmpty (struserpwd))7 {8Xtramessagebox.show ("Please enter user name and password","transmate", messageboxbuttons.retrycancel);9 }Ten Else One { A stringStrpostdata ="emailaddress="+ strUserName +"&password="+ struserpwd+""; - -HttpWebRequest HttpWebRequest = WebRequest.Create ("Http://192.168.1.130:30160/TransmateWebService/login") asHttpWebRequest; the -Httpwebrequest.keepalive =false; - - byte[] data =System.Text.Encoding.UTF8.GetBytes (strpostdata); + -Httpwebrequest.method ="POST"; + AHttpwebrequest.contentlength =data. Length; atHttpwebrequest.contenttype ="application/x-www-form-urlencoded"; -Stream newstream =Httpwebrequest.getrequeststream (); -Newstream.write (data,0, data. Length); - newstream.close (); - -HttpWebResponse response = HttpWebRequest.GetResponse () asHttpWebResponse; in -Stream Revicestream =Response. GetResponseStream (); toStreamReader StreamReader =NewStreamReader (REVICESTREAM,ENCODING.UTF8); + stringStrcontent =streamreader.readtoend (); - theJobject Jsonobject =Jobject.parse (strcontent); * stringLogincode = jsonobject["ErrorCode"]. ToString (); $ stringTipmessage = jsonobject["message"]. ToString ();Panax Notoginseng - if(Logincode = =" $") the { +Xtramessagebox.show ("Login successful, is jumping ...."); A } the Else + { -Xtramessagebox.show ("Login failed, please retry later"); $ } $}
Started debugging for a long time, the following error occurred:
After constant searching and debugging, the main thing is to write a short sentence:
1 " application/x-www-form-urlencoded ";
Hope to help you, follow-up will be HTTP, SOAP, TCP, UDP, Https, and other basic protocols to open a topic "Please look forward to ... 】
WinForm Client HTTP protocol and server-side communication