WinForm Client HTTP protocol and server-side communication

Source: Internet
Author: User

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

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.