1
Network / Re: sf::Http::Response::getField not returning all data
« on: August 27, 2013, 04:32:54 am »
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
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";
}
}
#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";
}
}