Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #include <string.h>
  3. #include <winsock2.h>
  4. #include <windows.h>
  5. #include <iostream>
  6. #include <vector>
  7. #include <locale>
  8. #include <sstream>
  9. #include "stdafx.h"
  10. #include <string>
  11. #include "curl/curl.h";
  12.  
  13. using namespace std;
  14.  
  15. #pragma comment(lib,"ws2_32.lib")
  16.  
  17. string HttpGet(string url,string port);
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. int main(void) {
  25.  
  26. string x = HttpGet("127.0.0.1" , "80");
  27.  
  28. cout << x;
  29.  
  30. cout << "\n\nPress ANY key to close.\n\n";
  31. cin.ignore(); cin.get();
  32.  
  33.  
  34.  
  35. return 0;
  36. }
  37.  
  38.  
  39.  
  40. string HttpGet(string url,string port) {
  41. char buffer[10000];
  42. int i = 0;
  43. string Response;
  44.  
  45.  
  46. WSADATA wsaData;
  47. SOCKET Socket;
  48. SOCKADDR_IN SockAddr;
  49. int lineCount = 0;
  50. int rowCount = 0;
  51. struct hostent *host;
  52. string header;
  53.  
  54.  
  55. string newl = "\n";
  56. //get_http = "GET /index.php HTTP/1.1\r\n " + newl + "Host: " + url + "\r\nConnection: close\r\n\r\n";
  57.  
  58.  
  59. header = "GET /index.php HTTP/1.1\r\n";
  60. header += "Host: " + url + ":" + port + "\r\n"; // we need to specify the port
  61. header += "Connection: close\r\n\r\n";
  62.  
  63. if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
  64. Response = "Failed.";
  65. }
  66.  
  67. Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  68. host = gethostbyname(url.c_str());
  69.  
  70. SockAddr.sin_port = htons(80);
  71. SockAddr.sin_family = AF_INET;
  72. SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
  73.  
  74. if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0) {
  75. Response = "Failed.";
  76. }
  77. send(Socket, header.c_str(), strlen(header.c_str()), 0);
  78.  
  79. int nDataLength;
  80. while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0) {
  81. int i = 0;
  82. while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
  83.  
  84. Response += buffer[i];
  85. i += 1;
  86. }
  87. }
  88. return Response;
  89. closesocket(Socket);
  90. WSACleanup();
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement