- 论坛徽章:
- 0
|
我想在Linux下使用Socket实现远程登陆一个HTTP服务器的功能。
找到了一份原来在Windows下实现的代码:- static TCHAR hdr_agent[] = _T("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; .NET CLR 1.1.4322)");
- static char * strPostData = "Host=192.168.100.163&LoginName=dGVzdA%3D%3D&LoginPassword=MTExMQ%3D%3D&LoginNameX=test&LoginPasswordX=1111";
- HINTERNET hSession = NULL,
- hConnect = NULL,
- hRequest = NULL;
- // Use WinHttpOpen to obtain a session handle.
- hSession = WinHttpOpen( hdr_agent,
- WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
- WINHTTP_NO_PROXY_NAME,
- WINHTTP_NO_PROXY_BYPASS, 0 );
- if( hSession )
- hConnect = WinHttpConnect( hSession, L"192.168.100.163", INTERNET_DEFAULT_HTTPS_PORT, 0 );
-
- //====================================================================================
- if( hConnect )
- hRequest = WinHttpOpenRequest( hConnect, L"POST", L"/SSL/user/PasswordUserLogin",
- NULL, WINHTTP_NO_REFERER,
- WINHTTP_DEFAULT_ACCEPT_TYPES,
- WINHTTP_FLAG_SECURE );
- DWORD dwSSLFlag;
- dwSSLFlag = SECURITY_FLAG_IGNORE_UNKNOWN_CA;
- dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
- dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
- dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
- WinHttpSetOption(hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &dwSSLFlag, sizeof(dwSSLFlag));
- // Send a request.
- if( hRequest )
- bResults = WinHttpSendRequest( hRequest,
- WINHTTP_NO_ADDITIONAL_HEADERS, 0,
- (LPVOID)strPostData, strlen(strPostData),
- strlen(strPostData), 0 );
复制代码 其中LoginName和LoginPassword是对LoginNameX和LoginPasswordX的Base64编码。
我参考这个写了一个请求:- static char *REQUEST_1 =
- "POST /SSL/user/PasswordUserLogin HTTP/1.1\r\n"
- "Content-Length:200\r\n"
- "User-Agent:Mozilla/4.0\r\n"
- "Host:%s&LoginName=dGVzdA%3D%3D&LoginPassword=MTExMQ%3D%3D&LoginNameX=test&LoginPasswordX=1111\r\n\r\n"
复制代码 但是我的请求总是得不到正确的响应。Windows下的Socket我不怎么会,大家帮忙看看是哪写得不对。谢谢~ |
|