Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,489 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,285 people online right now. Registration is fast and FREE... Join Now!




winsock http -.-

 
Reply to this topicStart new topic

winsock http -.-

okyup
1 Mar, 2007 - 03:59 PM
Post #1

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
Okay so I finally decided to learn to use sockets yesterday.

Think I got the basics down lol.

Can someone point out why this doesn't work? I think it has more to do with http headers and formatting than sockets. mad.gif I don't think it POSTs properly and it GETs some pages but doesn't get others. If you have any ideas I will research them, preferably not dive into the 130 page RFC as this stage though. ph34r.gif

CODE
#include "Socket.h"
#include <iostream>
#include <string.h>

using namespace std;

void getHPM(string &host,string &path, string &method, string &pdata){
    cout << "Host? " << endl;
    getline(cin,host);
    if (host=="exit" || host=="newsocket") return;
    cout << "Path? " << endl;
    getline(cin,path);
    cout << "Method? " << endl;
    getline(cin,method);
    if (method=="POST"){
        cout << "Post Data? " << endl;
        getline(cin,pdata);
    }
}

int main(int g) {
    cout << "g: " << g << endl;
    string host;
    string path;
    string method;
    string pdata;
    getHPM(host,path,method,pdata);
    SocketClient s(host.c_str(), 80);
    while (host!="exit" && host!="newsocket"){
        s.SendPartialLine(method.c_str());
        s.SendPartialLine(" ");
        s.SendPartialLine(path.c_str());
        s.SendLine(" HTTP/1.1");
        s.SendPartialLine("Host: ");
        s.SendLine(host.c_str());
        s.SendLine("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2");
        s.SendLine("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        s.SendLine("Accept-Language: en-us,en;q=0.5");
        //s.SendLine("Accept-Encoding: gzip,deflate");
        s.SendLine("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        //s.SendLine("Referer: http://www.nexopia.com/usercomments.php?cachekey=AH68K1546F");
        //s.SendLine("Cookie: flashInstalled=9.0  r28; userid=2294225; key=8b1518f916b47d47b02d3fb4f0d5afbd;");
        if (method=="POST"){
            s.SendLine("Content-Type: application/x-www-form-urlencoded");
            s.SendPartialLine("Content-Length: ");
            s.SendLine((char*)pdata.length());
            s.SendLine("");
            s.SendLine(pdata.c_str());
        }
        else
            s.SendLine("");
        while (1){
            string res = s.ReceiveLine();
            if (res.empty()) break;
            cout << res;
            cout.flush();
        }
        getHPM(host,path,method,pdata);
    }
    s.Close();
    if (host=="newsocket") return 1;
    else return 0;
}


And as you can see SendLine adds CRLF to what it sends. From what I can see you need one after a GET but it is forbidden after a POST? Oh damn lol. Might start in on the RFC after all. tongue.gif

Thanks for any insight. icon_up.gif

This post has been edited by okyup: 1 Mar, 2007 - 06:39 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:19PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month