Curl and redirects

Chris christian_stengel at yahoo.de
Mon Dec 17 03:24:40 PST 2012


Hi,

I am trying to log into a web application using curl. The web app
sends a 302 redirect after posting the data (successful log in -
I can see the successful login in the web apps log files).

Maybe I am doing something wrong - but I have just copied the
code of _basicHTTP form curl.d to my own method to authenticate.

The exception I'll receive on perform is:

std.net.curl.CurlException at std/net/curl.d(3348): Send failed
since rewinding of the data stream failed on handle 7FCABA819400

My Code:


       void post(string url, const(void)[] sendData){
           HTTP client = HTTP();
           client.method = HTTP.Method.post;

client.addRequestHeader("Content-Type","application/x-www-form-urlencoded");

           scope (exit) {
               client.onReceiveHeader = null;
               client.onReceiveStatusLine = null;
               client.onReceive = null;
           }
           client.url = url;
           HTTP.StatusLine statusLine;
           ubyte[] content;
           string[string] headers;
           client.onReceive = (ubyte[] data)
           {
               content ~= data;
               return data.length;
           };

           if (sendData !is null &&
               (client.method == HTTP.Method.post || client.method
== HTTP.Method.put))
           {
               client.contentLength = sendData.length;
               client.onSend = delegate size_t(void[] buf)
               {
                   size_t minLen = std.algorithm.min(buf.length,
sendData.length);
                   if (minLen == 0) return 0;
                   buf[0..minLen] = sendData[0..minLen];
                   sendData = sendData[minLen..$];
                   return minLen;
               };
           }

           client.onReceiveHeader = (in char[] key,
                                     in char[] value)
           {
               if (auto v = key in headers)
               {
                   *v ~= ", ";
                   *v ~= value;
               }
               else
                   headers[key] = value.idup;
           };
           client.onReceiveStatusLine = (HTTP.StatusLine l) {
statusLine = l; };
           client.perform();

           // Default charset defined in HTTP RFC
           auto charset = "ISO-8859-1";
           if (auto v = "content-type" in headers)
           {
               auto m = match(cast(char[]) (*v),
regex("charset=([^;,]*)"));
               if (!m.empty && m.captures.length > 1)
               {
                   charset = m.captures[1].idup;
               }
           }
       }

Does anybody have a clue, what I am doing wrong?

Thanks,

Christian


More information about the Digitalmars-d mailing list