Forum > Networking and Web Programming

how to do a HTTP POST with sockets

(1/4) > >>

Key-Real:
Hi,

I'm trying to send a SOAPAction to my Router.

It looks like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC} {$H+}uses    sockets, SysUtils; procedure SocketError(s:string);begin    writeln('Socket Error: ', s);    halt(1);end; const        MS : string =                  'POST /upnp/control/WANIPConnection0 HTTP/1.1' + #$0d + #$0a +                'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"' + #$0d + #$0a +                'Host: 192.168.0.4:49153' + #$0d + #$0a +                'Content-Type: text/xml' + #$0d + #$0a +                'Content-Length: '; var        socket : longint;        addr : sockaddr_in;        f : text;        post : string;        s : string;begin        assignfile(f, 'add.xml');        reset(f);        post:= '';        repeat                readln(f, s);                post:= post + s + #$0a + #$0d;        until eof(f);        close(f);         ms:=ms + intToStr(length(post)) + #$0d + #$0a + #$0d + #$0a + post + #$0d +#$0a;         writeln(ms);         socket:= fpsocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);        if socket = -1 then SocketError('Init Socket');         fillchar(addr, 0, sizeof(addr));        addr.sin_family:= AF_INET;        addr.sin_addr:= StrToNetAddr('192.168.0.1');        addr.sin_port:= htons(49153);         if fpconnect(socket, @addr, sizeof(addr)) < 0 then SocketError('unable to connect');         //if fpsendto(socket, pchar(MS), length(MS), 0, @addr, sizeof(addr)) = -1 then SocketError('send MS message');          if fpsend(socket, @ms, sizeof(ms), 0) < 0 then SocketError('unable to send');     closeSocket(socket);end.  

my add.xml:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---<?xml version="1.0"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body>    <m:AddPortMapping xmlns:m="urn:schemas-upnp-org:service:WANIPConnection:1">        <NewPortMappingDescription>It is my uPnP test</NewPortMappingDescription>        <NewLeaseDuration>0</NewLeaseDuration>        <NewInternalClient>192.168.0.4</NewInternalClient>        <NewEnabled>True</NewEnabled>        <NewExternalPort>3333</NewExternalPort>        <NewRemoteHost></NewRemoteHost>        <NewProtocol>UDP</NewProtocol>        <NewInternalPort>3333</NewInternalPort>    </m:AddPortMapping></SOAP-ENV:Body></SOAP-ENV:Envelope> 

so my question: Do I do the HTTP POST request right?

dbannon:
Have you considered  fcl-web ?

https://wiki.freepascal.org/fpWeb_Tutorial

In use, about line 1321 of https://github.com/tomboy-notes/tomboy-ng/blob/master/source/transgithub.pas where I talk to github with POST.

Davo

Warfley:
I don't know why you would want to implement HTTP yourself, but if you really want to do, to use HTTPS you must use TLS.

You can either directly use the openssl unit and use the functions like "sslConnect" instead of "fpConnect" (of course requires to setup an ssl context first, but thats just a bit of boilerplate code), as described here (for C): https://stackoverflow.com/questions/7698488/turn-a-simple-socket-into-an-ssl-socket

Or you can use the ssockets (+opensslsocket) units instead:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  ssockets, opensslsockets; var  Sock: TInetSocket;begin  Sock := TInetSocket.Create('127.0.0.1', 1337, TOpenSSLSocketHandler.create);  try    Sock.Connect;    Sock.Write('Hello World'[0], Length('Hello WOrld'));  finally    Sock.Free;  end;end.

edap:
Consider using mORMot2 unit https://github.com/synopse/mORMot2/blob/master/src/net/mormot.net.client.pas in folder https://github.com/synopse/mORMot2/tree/master/src

rvk:

--- Quote from: Warfley on June 03, 2023, 12:02:52 pm ---I don't know why you would want to implement HTTP yourself, but if you really want to do, to use HTTPS you must use TLS.

--- End quote ---
Seeing the code this is not https.
It's for UPnP to a router which is simple HTTP.

So just using THttpSend from Synapse or fcl-web is enough.

(I've done UPnP with THttpSend)

Navigation

[0] Message Index

[#] Next page

Go to full version