socket Http Post Data

int main() {

  try {
    SocketClient s("www.site.com", 80);
    string data = "par1=value1&par2=value2";
    s.SendLine("POST /post_page.php HTTP/1.0\r\n");
    s.SendLine("Host: www.site.com\r\n");
    s.SendLine("User-Agent: Mozilla/4.0\r\n");
    s.SendLine("Content-Length: 23\r\n\r\n");
    s.SendLine("Content-Type: application/x-www-form-urlencoded\r\n");
    s.SendLine("\r\n");
    s.SendLine(data + "\r\n");
    s.SendLine("\r\n");


    while (1) {
      string l = s.ReceiveLine();
      if (l.empty()) break;
      cout << l;
      cout.flush();
    }

  }
  catch (const char* s) {
    cerr << s << endl;
  }
  catch (std::string s) {
    cerr << s << endl;
  }
  catch (...) {
    cerr << "unhandled exception\n";
  }

  return 0;
}

 

posted @ 2013-05-07 10:28  wiessharling  阅读(198)  评论(0编辑  收藏  举报