Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting redirect information with C# and C++
    primarykey
    data
    text
    <p><br>What am I doing wrong? I'm trying to get redirect information from a server that I use for authenticating a user. This works good with my C# code but I cannot make my C++ code working. <br>So here's the working C# code:</p> <pre><code>string postData = @"username=myUsername&amp;serviceID=someServiceId&amp;password=somePassword"; byte[] postBytes = Encoding.UTF8.GetBytes(postData); // create the proper HttpWebRequest HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://myAuthServer.com/login"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "utf-8"; request.ContentLength = postBytes.Length; request.PreAuthenticate = true; request.AllowAutoRedirect = false; // now get the information from the login server Stream requestStream = request.GetRequestStream(); HttpWebResponse response = null; // post the data requestStream.Write(postBytes, 0, postBytes.Length); // get the response and get the wanted information response = (HttpWebResponse)request.GetResponse(); const string Session = "?session="; Uri location = new Uri(response.Headers["Location"]); m_redirect = location.Host + location.LocalPath; m_authentificationId = location.Query.Substring(Session.Length); </code></pre> <p>I use these information for further authentification. Everything is fine. <br><br>I now want to do the same with C++ code. But that code doesn't work. <br>So here is:</p> <pre><code>HINTERNET hOpen, hConnect, hReq; hOpen = InternetOpen(_T(""), INTERNET_OPEN_TYPE_DIRECT, _T(""), _T(""), 0); if(!InternetHandleValid(hOpen)) { return false; } hConnect = InternetConnect(hOpen, _T("myAuthServer.com"), INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if(!InternetHandleValid(hConnect)) { return false; } hReq = HttpOpenRequest(hConnect, _T("POST"), _T("/login"), NULL, NULL, NULL, INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_SECURE, 0); if(!InternetHandleValid(hReq)) { return false; } if (!HttpAddRequestHeaders(hReq, _T("Content-Type: application/x-www-form-urlencoded"), -1L, HTTP_ADDREQ_FLAG_ADD)) { return false; } if (!HttpAddRequestHeaders(hReq, _T("Expect: 100-continue"), -1L, HTTP_ADDREQ_FLAG_ADD)) { return false; } if (!HttpAddRequestHeaders(hReq, _T("Connection: Keep-Alive"), -1L, HTTP_ADDREQ_FLAG_ADD)) { return false; } if (!HttpAddRequestHeaders(hReq, _T("Accept: utf-8"), -1L, HTTP_ADDREQ_FLAG_ADD)) { return false; } CString cstrPostData = _T("username=myUsername&amp;serviceID=someServiceId&amp;password=somePassword"); char buffer[10]; _itoa_s(cstrPostData.GetLength(), buffer, 10, 10); CString cstrContentLength(buffer); if (!HttpAddRequestHeaders(hReq, _T("Content-Length: ") + cstrContentLength, -1L, HTTP_ADDREQ_FLAG_ADD)) { return false; } LPVOID lpData = (LPVOID)cstrPostData.GetBuffer(cstrPostData.GetLength()); if(HttpSendRequest(hReq, NULL, -1L, lpData, wcslen(cstrPostData))) { DWORD dwCode, dwCodeSize; dwCodeSize = sizeof(DWORD); if(!HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &amp;dwCode, &amp;dwCodeSize, NULL)) { return false; } // we need to have the 302 (Redirect) here if (dwCode != 302) { return false; } wchar_t chData[1024] = _T("\0"); DWORD dwBuffer = 1023; if(!HttpQueryInfo(hReq, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)chData, &amp;dwBuffer, NULL)) { return false; } CStringA cstrHeaders(chData); bool b = ExtractRedirectAndSessionFromHeader(cstrHeaders); } </code></pre> <p>Problem now is that I get <code>Location: https://myAuthServer.com/centrallogin-2.2/error.jsp</code> so no proper redirect and session. I already found out that I also get this error in my C# code when I don't set <code>request.ContentType</code> properly. But I did set the Content-Type header in my C++ code. So I have no idea what I do wrong. <br>Any further ideas? <br>EDIT: <br>Ok, I also tried it with the WinHTTP API. Here's my code:</p> <pre><code>HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL; // Use WinHttpOpen to obtain a session handle. hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); hConnect = WinHttpConnect(hSession, L"myAuthServer.com", INTERNET_DEFAULT_HTTPS_PORT, 0); hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/login", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); // disable auto redirects DWORD dwOptionValue = WINHTTP_DISABLE_REDIRECTS; BOOL b = WinHttpSetOption(hRequest, WINHTTP_OPTION_DISABLE_FEATURE, &amp;dwOptionValue, sizeof(dwOptionValue)); static const WCHAR szContentType[] = L"Content-Type: application/x-www-form-urlencoded\r\n"; b = WinHttpAddRequestHeaders(hRequest, szContentType, (DWORD)-1, WINHTTP_ADDREQ_FLAG_ADD &amp; WINHTTP_ADDREQ_FLAG_REPLACE); CString cstrPostData = _T("username=myUsername&amp;serviceID=someServiceId&amp;password=somePassword"); b = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, -1L, WINHTTP_NO_REQUEST_DATA, 0, wcslen(cstrPostData), 0); DWORD dwBytesWritten = 0; b = WinHttpWriteData(hRequest, cstrPostData, wcslen(cstrPostData), &amp;dwBytesWritten); b = WinHttpReceiveResponse(hRequest, NULL); DWORD dwSize = sizeof(DWORD); DWORD dwStatusCode = 0; b = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &amp;dwStatusCode, &amp;dwSize, WINHTTP_NO_HEADER_INDEX); wchar_t chData[1024] = _T("\0"); DWORD dwBuffer = 1023; b = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF, NULL, (LPVOID)chData, &amp;dwBuffer, NULL); CStringA cstrHeaders(chData); WinHttpCloseHandle(hSession); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hRequest); cstrPostData.ReleaseBuffer(); </code></pre> <p>So the redirect url and the session id should be in chData. But again the result is wrong. I also tried Wireshark. As this all is HTTPS I cannot read much out of it. Only thing I could compare is the number of frames etc. When using WinHTTP the Wireshark log looks similar. So I think this is the better approach. <br>Any further ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload