Hi all i just installed sfml yesterday and im completely loving it! My only issue is that when i call getField("Set-Cookie") not all the data is returned, only 2 of 4-6 cookies are printed. When I inspect the Set-Cookie after doing the same request w/ C# WebClient its much more populated.
sfml code
std::string GetCookie(std::string username,std::string pass)
{
sf::Http http("http://www.nexon.net/");
sf::Http::Request request("/api/v001/account/login",sf::Http::Request::Post);
request.setHttpVersion(1,1);
request.setField("Content-Type", "aanpplication/x-www-form-urlencoded");
request.setField("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
std::ostringstream stream;
stream << "userID=" << username << "&password=" << pass;
request.setBody(stream.str());
sf::Http::Response response = http.sendRequest(request);
if (response.getStatus() == sf::Http::Response::Ok)
{
prtinf("%s\n",response.getField("Set-Cookie").c_str());
return response.GetField("Set-Cookie");
}
else
{
Log("HttpResponse is not sf::Http::Response::Ok\n");
}
return std::string("error");
}
c# code
static string GetAuth
(string username ,
string password , IWebProxy proxy
= null) { WebClient wc
= new WebClient
(); wc
.Headers.Add("Content-Type",
"application/x-www-form-urlencoded"); wc
.Headers.Add("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); wc
.Proxy = proxy
; //if proxy isnt null it will try to use the default one string rtn
= "@error"; try { wc
.UploadString("http://www.nexon.net/api/v001/account/login",
string.Format("userID={0}&password={1}", username , password
)); rtn
= wc
.ResponseHeaders["Set-Cookie"]; } catch (Exception
) { } //exception handler finally //dipose of resource { if (wc
!= null) wc
.Dispose(); } return rtn
; }
Does anyone know why this is?
Hi i ended up creating my own Http class based off of the code on git for this, its not 100% sanitary but it gets the job done
code is anyone cares
#include "Auth.h"
#include "Log.h"
#include <sstream>
#include <SFML/Network.hpp>
class HttpPostRequest
{
public:
static const int MajorHttpVersion = 1;
static const int MinorHttpVersion = 0;
HttpPostRequest(std::string host, std::string uri)
{
m_host = host;
m_uri = uri;
}
void AddHeader(std::string field,std::string value)
{
std::ostringstream out;
out << field << ": " << value;
m_headers.push_back(out.str());
}
void SetBody(std::string body)
{
m_body = body;
}
std::string GetResponse()
{
return m_response;
}
bool Send()
{
sf::TcpSocket socket;
if(socket.connect(m_host,80,sf::seconds(10)) == sf::Socket::Done)
{
std::string data = Prepare();
if(socket.send(data.c_str(),data.size()) == sf::Socket::Done)
{
std::string receivedStr;
std::size_t size = 0;
char buffer[1024];
while (socket.receive(buffer, sizeof(buffer), size) == sf::Socket::Done)
{
receivedStr.append(buffer, buffer + size);
}
m_response = receivedStr;
}
socket.disconnect();
return true;
}
return false;
}
private:
std::string Prepare()
{
std::ostringstream out;
// Write the first line containing the request type
out << "POST " << m_uri << " HTTP/" << MajorHttpVersion << "." << MinorHttpVersion << "\r\n";
// Write header
for(auto& header : m_headers)
{
out << header << "\r\n";
}
// Use an extra \r\n to separate the header from the body
out << "Content-Length: " << m_body.size() << "\r\n\r\n" << m_body;
return out.str();
}
std::string m_host, m_uri, m_body, m_response;
std::vector<std::string> m_headers;
};
std::string GetCookie(std::string username,std::string pass)
{
std::string host = "nexon.net";
HttpPostRequest request(host,"/api/v001/account/login");
request.AddHeader("Content-Type","application/x-www-form-urlencoded");
request.AddHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
request.AddHeader("Host",host);
std::ostringstream stream;
stream << "userID=" << username << "&password=" << pass;
request.SetBody(stream.str());
if(request.Send())
{
std::string result = request.GetResponse();
std::string::size_type pos = result.find("NPPv2=");
if (pos != std::string::npos)
{
result.erase(0,pos + 6);
return result.substr(0,result.find(";"));
}
else
{
Log("No NPPv2 in response\n");
return "error";
}
}
else
{
Log("HttpPostRequest failed\n");
return "error";
}
}