sockets - 如何在fiddler中捕获套接字编程的发送和接收请求

标签 sockets c++11 visual-c++ tcp

我有在 C++ 中创建套接字的代码。代码运行良好。代码是:

#include <winsock2.h>
#include <WS2tcpip.h>
#include <windows.h>
#include <iostream>    
#pragma comment(lib,"ws2_32.lib") using namespace std;
int main (){
// Initialize Dependencies to the Windows Socket.
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  cout << "WSAStartup failed.\n";
  system("pause");
  return -1;
}

// We first prepare some "hints" for the "getaddrinfo" function
// to tell it, that we are looking for a IPv4 TCP Connection.
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;          // We are targeting IPv4
hints.ai_protocol = IPPROTO_TCP;    // We are targeting TCP
hints.ai_socktype = SOCK_STREAM;    // We are targeting TCP so its SOCK_STREAM

// Aquiring of the IPv4 address of a host using the newer
// "getaddrinfo" function which outdated "gethostbyname".
// It will search for IPv4 addresses using the TCP-Protocol.
struct addrinfo* targetAdressInfo = NULL;
DWORD getAddrRes = getaddrinfo("www.google.com", NULL, &hints, &targetAdressInfo);
if (getAddrRes != 0 || targetAdressInfo == NULL)
{
  cout << "Could not resolve the Host Name" << endl;
  system("pause");
  WSACleanup();
  return -1;
}

// Create the Socket Address Informations, using IPv4
// We dont have to take care of sin_zero, it is only used to extend the length of SOCKADDR_IN to the size of SOCKADDR
SOCKADDR_IN sockAddr;
sockAddr.sin_addr = ((struct sockaddr_in*) targetAdressInfo->ai_addr)->sin_addr;    // The IPv4 Address from the Address Resolution Result
sockAddr.sin_family = AF_INET;  // IPv4
sockAddr.sin_port = htons(80);  // HTTP Port: 80

// We have to free the Address-Information from getaddrinfo again
freeaddrinfo(targetAdressInfo);

// Creation of a socket for the communication with the Web Server,
// using IPv4 and the TCP-Protocol
SOCKET webSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (webSocket == INVALID_SOCKET)
{
  cout << "Creation of the Socket Failed" << endl;
  system("pause");
  WSACleanup();
  return -1;
}

// Establishing a connection to the web Socket
cout << "Connecting...\n";
if(connect(webSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) != 0)
{
  cout << "Could not connect";
  system("pause");
  closesocket(webSocket);
  WSACleanup();
  return -1;
}
cout << "Connected.\n";

// Sending a HTTP-GET-Request to the Web Server
const char* httpRequest = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
int sentBytes = send(webSocket, httpRequest, strlen(httpRequest),0);
if (sentBytes < strlen(httpRequest) || sentBytes == SOCKET_ERROR)
{
  cout << "Could not send the request to the Server" << endl;
  system("pause");
  closesocket(webSocket);
  WSACleanup();
  return -1;
}

// Receiving and Displaying an answer from the Web Server
char buffer[10000];
ZeroMemory(buffer, sizeof(buffer));
int dataLen;
while ((dataLen = recv(webSocket, buffer, sizeof(buffer), 0) > 0))
{
  int i = 0;
  while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
    cout << buffer[i];
    i += 1;
  }
}

// Cleaning up Windows Socket Dependencies
 closesocket(webSocket);
 WSACleanup();

 system("pause");
 return 0; }

我想在发送和接收请求时在 fiddler 中捕获请求和响应,但 fiddler 没有捕获它。 提前致谢

最佳答案

Fiddler 将自己作为 HTTP 代理服务器插入到堆栈中。它依靠 Web 浏览器识别 PC 上配置的代理并通过它发送。您的代码未检测到要发送的代理 - 因此 Fiddler 将无法监控您的流量。

您有多种选择。

  1. 由于您是自己的 Windows,只需从使用直接套接字切换到使用 WinInet HTTP API。它将为您自动检测代理,无需您考虑。如果需要,它也会进行代理身份验证。

  2. 或。使用 WiresharkNetMon来分析您的流量而不是 Fiddler。

我推荐 #1,因为这意味着您的代码将在存在真实代理服务器(通常在企业网络中找到)的情况下工作,而 Fiddler 将只使用它。

我想还有第三种选择,您可以自动检测浏览器代理设置,然后创建到代理的套接字,使用 HTTP 代理协议(protocol)等...但这不是最佳做法。

关于sockets - 如何在fiddler中捕获套接字编程的发送和接收请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29769598/

相关文章:

windows - win32 API SearchPath 失败

c++ - 通过 'tuple' 和 'tie' 实现比较运算符,好主意吗?

C++模板继承隐藏模板参数

c++ 使用 SetCursor 设置游标

asp.net - 具有 redis 背板横向扩展的 Web 套接字 - 每个用户多个 redis channel 或所有用户一个 redis channel

Python Web 服务器套接字

c# - 将 Socket.ReceiveAsync 替换为 NetworkStream.ReadAsync(可等待)

c++ - recv 传入 0 以检测套接字错误是否安全?

c++ - 如何防止 main() 在单独的线程访问它时更改变量

c++ - 编辑控件中的搜索图标与输入区域重叠