C # Use SOCKET to send an HTTP request example

Source: Internet
Author: User
  1. Call Code
  2. Private void button#click (Object sender, eventargs E)
  3. {
  4. String urlstr = This. texturl. text;
  5. If (urlstr = NULL | "". Equals (urlstr ))
  6. {
  7. MessageBox. Show ("the address to be asked! ");
  8. Return;
  9. }
  10. Int Port = 80;
  11. Try
  12. {
  13. Port = convert. toint32 (this. textport. Text );
  14. }
  15. Catch
  16. {
  17. MessageBox. Show ("enter the correct port ");
  18. }
  19. // Can be empty
  20. String Path = "/" + this. textfile. text;
  21. This. textbox1.text = "";
  22. This. Conn. startaccess (urlstr, port, path, this. textbox1 );
  23. }

 

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Using system. net;
  6. Using system. net. Sockets;
  7. Using system. Windows. forms;
  8. Namespace windowsformsapplication3
  9. {
  10. Class myconnection
  11. {
  12. Public void startaccess (string urlstr, int port, string path, textbox List)
  13. {
  14. Byte [] DATA = new byte [1024];
  15. Iphostentry gist = DNS. gethostbyname (urlstr );
  16. // Obtain the IP address of the accessed URL
  17. IPaddress IP = gist. Addresslist [0];
  18. Ipendpoint ipend = new ipendpoint (IP, Port );
  19. // Use the TCP stream type (IPv4)
  20. Socket socket = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
  21. Try
  22. {
  23. Socket. Connect (ipend );
  24. }
  25. Catch (socketexception E)
  26. {
  27. MessageBox. Show ("An error occurred while connecting to the accessed server! ");
  28. Console. Write (E. tostring ());
  29. Return;
  30. }
  31. // The relative address of the request. This parameter is optional if you access www.baidu.com directly. It can be blank.
  32. Stringbuilder Buf = new stringbuilder ();
  33. Buf. append ("get"). append (PATH). append ("HTTP/1.0/R/N ");
  34. Buf. append ("Content-Type: Application/X-WWW-form-urlencoded/R/N ");
  35. Buf. append ("/R/N ");
  36. Byte [] MS = system. Text. utf8encoding. utf8.getbytes (BUF. tostring ());
  37. // Send
  38. Socket. Send (MS );
  39. Int Recv = 0;
  40. Do
  41. {
  42. Recv = socket. Receive (data );
  43. // If the requested page meta specifies that the encoding of the page is gb2312, you need to use the corresponding encoding to convert bytes.
  44. // List. Text + = (encoding. utf8.getstring (data, 0, Recv ));
  45. List. Text + = (encoding. Default. getstring (data, 0, Recv ));
  46. } While (Recv! = 0 );
  47. // Disable last sending and receiving
  48. Socket. Shutdown (socketshutdown. Both );
  49. Socket. Close ();
  50. }
  51. }
  52. }

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.