C ++ / CLI HttpWebRequest :: GetRequestStream()在第一次失败后失败

时间:2014-04-24 20:08:01

标签: c++ httpwebrequest response dispose command-line-interface

我必须在我的一个程序中使用C ++ / CLI,但我以前从未使用过它。所以它给了我一些问题。这基本上是一个向登录页面发送http请求的功能。它适用于我尝试的第一个用户,但对于第二个用户,它在GetRequestStream()上失败。我搜索了这个,大多数主题都是关于删除/处理它。我相信我现在正在做这件事吗?在超时到来之前我仍需要等待20秒(Time-out excpetion)。我再也没有想法如何解决这个问题了。

bool GetAuthKey(std::string _username, std::string _pass, std::string& _key)
{
System::String ^ username = gcnew String(_username.c_str());
System::String ^ pass = gcnew String(_pass.c_str());

System::Net::CookieContainer ^cookieContainer = gcnew System::Net::CookieContainer();

System::Net::HttpWebRequest ^first_request = safe_cast<System::Net::HttpWebRequest^>(System::Net::HttpWebRequest::Create("https://passport.nexoneu.com/login/"));
first_request->CookieContainer = cookieContainer;
first_request->Method = "GET";
first_request->KeepAlive = true;
first_request->GetResponse();

System::Net::Cookie ^theme = gcnew System::Net::Cookie("theme", "NexonEU");
theme->Domain = ".nexoneu.com";
cookieContainer->Add(theme);

/*ArrayWriter ^postData = gcnew ArrayWriter();
postData.WriteBytes(Encoding.UTF8.GetBytes("{\"account\":{\"userId\":\"" + username + "\",\"password\":\""));
postData.WriteBytes(pass);// changed
postData.WriteBytes(Encoding.UTF8.GetBytes("\",\"accessedGame\":\"NexonEU\",\"captcha\":null,\"isSaveID\":true}}"));*/

String ^data = "{\"account\":{\"userId\":\"" + username + "\",\"password\":\"" + pass + "\",\"accessedGame\":\"NexonEU\",\"captcha\":null,\"isSaveID\":true}}";
array<Byte> ^buffer = Encoding::UTF8->GetBytes(data);

System::Net::HttpWebRequest ^request = safe_cast<System::Net::HttpWebRequest^>(System::Net::HttpWebRequest::Create("https://passport.nexoneu.com/Service/Authentication.asmx/Login"));
request->Method = "POST";
request->CookieContainer = cookieContainer;
request->Date = DateTime::UtcNow;
request->ServicePoint->Expect100Continue = false;// think ive to change this

request->Accept = "application/json, text/javascript, */*; q=0.01";
request->KeepAlive = true;
request->ContentLength = buffer->Length;
request->ContentType = "application/json; charset=UTF-8";
request->Host = "passport.nexoneu.com";
request->Referer = "https://passport.nexoneu.com/login/";
request->UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36";

request->Headers->Add("Accept-Encoding", "gzip,deflate,sdch");
request->Headers->Add("Accept-Language", "en-US;q=0.6,en;q=0.4");
request->Headers->Add("X-Requested-With", "XMLHttpRequest");
request->Headers->Add("DNT", "1");
request->Headers->Add("Origin", "https://" + request->Host);


System::IO::Stream ^stream = request->GetRequestStream(); // this crashes
stream->Write(buffer, 0, buffer->Length);

stream->Flush();
stream->Close();
delete stream;

System::Net::HttpWebResponse ^response = safe_cast<System::Net::HttpWebResponse^>(request->GetResponse());

for each(System::Net::Cookie ^cookie in response->Cookies)
{
    if (cookie->Name->ToUpper()->Equals("NPP"))
    {
        msclr::interop::marshal_context context;

        _key = context.marshal_as<std::string>(cookie->Value);
        break;
    }
}

request->Abort();
response->Close();

delete request;
delete response;

return !_key.empty();

}

1 个答案:

答案 0 :(得分:0)

删除此行:delete stream;

在C ++ / CLI中,如果托管对象实现了System :: IDisposable接口,则删除会调用托管对象上的Dispose方法,而Stream会实现它。

但是代码已经在该流上调用了close()函数,close()函数也调用了Dispose方法。所以代码在该流上调用两次Dispose(),我认为这就是你错误的原因。