C + + uses HTTP requests, pro-Test available, VS2008 compiled run through

Source: Internet
Author: User

[CPP]View PlainCopy
  1. int cmyfunctionsdlg::request (char* hostname, char* API, char* parameters)
  2. {
  3. Wsadata Wsadata;
  4. WSAStartup (0x0101, &wsadata);
  5. //Initialize socket
  6. struct hostent* host_addr = gethostbyname (hostname);
  7. if (host_addr = = NULL)
  8. {
  9. cout<< "Unable to locate host" <<endl;
  10. return-103;
  11. }
  12. Sockaddr_in sin;
  13. sin.sin_family = af_inet;
  14. Sin.sin_port = htons ((unsigned short ) 80);
  15. SIN.SIN_ADDR.S_ADDR = * ((int*) *host_addr->h_addr_list);
  16. int sock = socket (af_inet, sock_stream, 0);
  17. if (sock = =-1)
  18. {
  19. return-100;
  20. }
  21. //Establish connection
  22. if (Connect (sock, (const struct sockaddr *) &sin, sizeof (sockaddr_in)) = =-1)
  23. {
  24. cout<< "Connect Failed" <<endl;
  25. return-101;
  26. }
  27. //Initialize Send message
  28. char send_str[2048] = {0};
  29. //Header information
  30. strcat (Send_str, "POST");
  31. Strcat (Send_str, API);
  32. strcat (Send_str, "http/1.1\r\n");
  33. strcat (Send_str, "Host:");
  34. strcat (SEND_STR, hostname);
  35. strcat (send_str, "\ r \ n");
  36. strcat (Send_str, "connection:keep-alive\r\n");
  37. Char content_header[100];
  38. sprintf (Content_header,"content-length:%d\r\n", strlen (parameters));
  39. strcat (Send_str, Content_header);
  40. strcat (Send_str, "cache-control:max-age=0\r\n");
  41. strcat (Send_str, "origin:http://www.hao123.com\r\n");
  42. strcat (Send_str, "user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/535.1 (khtml, like Gecko) chrome/15.0.849.0 Sa  Fari/535.1\r\n ");
  43. strcat (Send_str, "content-type:application/x-www-form-urlencoded\r\n");
  44. strcat (Send_str, "accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
  45. strcat (Send_str, "referer:http://www.hao123.com/\r\n");
  46. strcat (Send_str, "accept-encoding:gzip,deflate,sdch\r\n");
  47. strcat (Send_str, "accept-language:zh-cn,zh;q=0.8\r\n");
  48. strcat (Send_str, "expect:100-continue\r\n");
  49. //Content Information
  50. strcat (send_str, "\ r \ n");
  51. strcat (send_str, parameters);
  52. if (send (sock, Send_str, strlen (SEND_STR), 0) = =-1)
  53. {
  54. cout<< "Send Failed" <<endl;
  55. return-101;
  56. }
  57. //Get return information
  58. char recv_str[4096] = {0};
  59. if (recv (sock, Recv_str, sizeof (RECV_STR), 0) = =-1)
  60. {
  61. cout<< "Recv failed" <<endl;
  62. return-101;
  63. }
  64. cout<<recv_str<<endl;
  65. WSACleanup ();
  66. return 0;
  67. }

Request ("www.xxxx.com", "http://www.xxxx.com/client/data.php", "{\" request\ ": \" userlogin\ ", \" Posts\ ": [{\" Loginname\ ": \" 123456\ ", \" password\ ": \" 7890\ "}]}");

http://blog.csdn.net/dingxz105090/article/details/41285149

C + + uses HTTP requests, pro-Test available, VS2008 compiled run through

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.