我正在尝试从托管c ++中的应用程序登录我的网站。我使用curl执行此操作,虽然我没有收到任何错误,但我对如何获取检查登录是否成功所需的信息感到困惑。不幸的是,我无法找到与我有类似问题的人,所以我感谢任何帮助。
private: System::Void bLogin_Click ( System::Object^ sender, System::EventArgs^ e )
{
if ( System::String::IsNullOrEmpty ( tbUsername->Text ) )
{
System::String^ str = gcnew System::String ( "Login failed! Please enter a valid username." );
System::Windows::Forms::MessageBox::Show ( str );
return;
}
if ( System::String::IsNullOrEmpty ( tbUsername->Text ) )
{
System::String^ str = gcnew System::String ( "Login failed! Please enter a valid password" );
System::Windows::Forms::MessageBox::Show ( str );
return;
}
using System::Runtime::InteropServices::Marshal;
curl_global_init ( CURL_GLOBAL_ALL );
CURL* curl = curl_easy_init ();
if ( !curl )
{
curl_global_cleanup ();
return;
}
curl_easy_setopt ( curl, CURLOPT_SSL_VERIFYPEER, 1L );
curl_easy_setopt ( curl, CURLOPT_SSL_VERIFYHOST, 1L );
curl_easy_setopt ( curl, CURLOPT_USERAGENT, "Mozilla/4.0" );
curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt ( curl, CURLOPT_URL, "https://www.myurl.com/member.php?action=login" );
const char* szUser = static_cast< const char* >( Marshal::StringToHGlobalAnsi ( tbUsername->Text ).ToPointer () );
const char* szPass = static_cast< const char* >( Marshal::StringToHGlobalAnsi ( tbPassword->Text ).ToPointer () );
curl_easy_setopt ( curl, CURLOPT_POST, 1L );
curl_easy_setopt ( curl, CURLOPT_POSTFIELDS, Fmt::Fmt ( "username=%s&password=%s", szUser, szPass ) );
//curl_easy_setopt ( curl, CURLOPT_USERPWD, Fmt::Fmt ( "%s:%s", szUser, szPass ) );
std::string strData;
curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, WriteCallback );
curl_easy_setopt ( curl, CURLOPT_WRITEDATA, &strData );
CURLcode res = curl_easy_perform ( curl );
/* its always CURLE_OK */
if ( res != CURLE_OK )
{
System::Windows::Forms::MessageBox::Show ( "A error has occured during the login process." );
}
Marshal::FreeHGlobal ( System::IntPtr ( reinterpret_cast< long long >( szUser ) ) );
Marshal::FreeHGlobal ( System::IntPtr ( reinterpret_cast< long long >( szPass ) ) );
/* check if suceeded here... but everything in strData is the same whether the credentials are right or wrong */
curl_easy_cleanup ( curl );
curl_global_cleanup ();
}
此外,我的网站附带了#34; Wildcard SSL&#34;但我不确定这是否重要? 我对所有这一切都很陌生,所以如果我做错了什么,我很抱歉。
答案 0 :(得分:0)
区分成功登录和失败登录完全取决于服务器提供的答复。由于我们不知道涉及哪个Web应用程序,因此我们无法提供更准确的信息。
其次,使用托管c ++项目中的curl非常奇怪。您可以访问.NET框架,该框架提供更易于使用的HTTP客户端,避免了所有手动清理/封送。
答案 1 :(得分:0)
我最终使用了WebRequest类,正如D. Jurcau建议的那样。另外,我将action=do_login
添加到参数中,然后检查字符串是否成功