|
|
Home » U++ Library support » U++ MT-multithreading and servers » HttpRequest File Upload
HttpRequest File Upload [message #39557] |
Fri, 29 March 2013 13:23  |
Brat
Messages: 7 Registered: March 2013
|
Promising Member |
|
|
Hello,
i want to upload an file to Rapidshare with HttpRequest.
Is there an example how to create a POST Request? And an example how to upload a file?
I found this code, but i dont whether it works or not and how to add other post parameters (like username, password)
String boundary = "BbX8c0";
StringBuffer b;
b.Cat("--" + boundary + "\r\n");
b.Cat("Content-Disposition: form-data; name=\"file\"; filename=\"" + file + "\"\r\n\r\n");
b.Cat(fi);
b.Cat("\r\n--" + boundary + "--\r\n");
//EDIT: Okay, now i know what was wrong. Right code is
HttpRequest GetServer("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver");
ulserver = GetServer.Execute();
if(!GetServer.IsSuccess()){
PromptOK("Couldn find uploadserver");
return;
}
HttpRequest req("http://rs" + ulserver + ".rapidshare.com/cgi-bin/rsapi.cgi");
req.POST();
req.Post("sub", "upload");
req.Post("login", "NAME");
req.Post("password", "PASSWORD");
req.Post("filename", file);
req.Post("filecontent", LoadFile(file));
String s = req.Execute();
[Updated on: Fri, 29 March 2013 14:49] Report message to a moderator
|
|
|
Re: HttpRequest File Upload [message #39566 is a reply to message #39557] |
Sat, 30 March 2013 11:50   |
Brat
Messages: 7 Registered: March 2013
|
Promising Member |
|
|
Okay,
seems like the code only works because the Rapidshare API allows to override the filename with the "filname" parameter.
When I wand to upload a file to share-online.biz, i cant pass the filename as an parameter.
void fUpload::Upload(){
Vector <String> session; // to store SESSION and ServerURL
int64 Filesize;
String ret;
FileIn fi;
if(file == ""){
PromptOK("No File selected");
return;
}
HttpRequest GetServer("http://www.share-online.biz/upv3_session.php");
GetServer.POST();
GetServer.Post("username", "USER");
GetServer.Post("password", "PW");
ret = GetServer.Execute();
if(!GetServer.IsSuccess()){
PromptOK("Couldnt find uploadserver");
return;
}
session = Split(ret, ";");
if(!fi.Open(file)){
PromptOK("Couldnt open " + file);
return;
}
Filesize = fi.GetSize();
fi.Close();
HttpRequest req(session[1]);
req.POST();
req.Post("username", "USER");
req.Post("password", "PW");
req.Post("upload_session", session[0]);
req.Post("chunk_no", "1");
req.Post("chunk_number", "1");
req.Post("filesize", AsString(Filesize));
req.Post("fn", LoadFile(file));
req.Post("finalize", "1");
ret = req.Execute();
if(!req.IsSuccess()){
PromptOK("FAILED");
PromptOK(req.GetErrorDesc());
return;
}
PromptOK(ret);
}
The second request doesnt work cause there is no filename parameter. How can i pass the filename without a filename parameter?
|
|
|
Re: HttpRequest File Upload [message #39568 is a reply to message #39566] |
Sat, 30 March 2013 12:13   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
Brat wrote on Sat, 30 March 2013 06:50 | Okay,
seems like the code only works because the Rapidshare API allows to override the filename with the "filname" parameter.
When I wand to upload a file to share-online.biz, i cant pass the filename as an parameter.
void fUpload::Upload(){
Vector <String> session; // to store SESSION and ServerURL
int64 Filesize;
String ret;
FileIn fi;
if(file == ""){
PromptOK("No File selected");
return;
}
HttpRequest GetServer("http://www.share-online.biz/upv3_session.php");
GetServer.POST();
GetServer.Post("username", "USER");
GetServer.Post("password", "PW");
ret = GetServer.Execute();
if(!GetServer.IsSuccess()){
PromptOK("Couldnt find uploadserver");
return;
}
session = Split(ret, ";");
if(!fi.Open(file)){
PromptOK("Couldnt open " + file);
return;
}
Filesize = fi.GetSize();
fi.Close();
HttpRequest req(session[1]);
req.POST();
req.Post("username", "USER");
req.Post("password", "PW");
req.Post("upload_session", session[0]);
req.Post("chunk_no", "1");
req.Post("chunk_number", "1");
req.Post("filesize", AsString(Filesize));
req.Post("fn", LoadFile(file));
req.Post("finalize", "1");
ret = req.Execute();
if(!req.IsSuccess()){
PromptOK("FAILED");
PromptOK(req.GetErrorDesc());
return;
}
PromptOK(ret);
}
The second request doesnt work cause there is no filename parameter. How can i pass the filename without a filename parameter?
|
I guess it this is really service specific, anyway I would dare to say that alternative to passing filename parameter would be "RESTfull" - pass it as part of url. Perhaps something like
HttpRequest req(session[1] + '/' + filename);
Anyway, once again, there is not firm standard...
Mirek
|
|
|
|
|
Re: HttpRequest File Upload [message #39573 is a reply to message #39571] |
Sat, 30 March 2013 21:11   |
Brat
Messages: 7 Registered: March 2013
|
Promising Member |
|
|
Okay,
i have to calculate the new Content-Length - how can i do this? strlen()? And how can i manipulate the Content-Length field?
#EDIT:
Okay, i created this class.
class MultiHTTP {
public:
MultiHTTP();
MultiHTTP(String boundary);
String GetContentType();
String GetBoundary();
void AddData(String name, String content);
void AddFile(String name, String filename, String mime, String content);
String GetRequest();
private:
String bound;
String req;
int64 Length;
};
MultiHTTP::MultiHTTP(){
bound = "---MULTIPART-BOUNDARY---" + AsString(Random()) + "---" + AsString(Random()) + "---";
req = bound + "\n";
}
MultiHTTP::MultiHTTP(String boundary){
bound = boundary;
req = bound + "\n";
}
String MultiHTTP::GetContentType(){
return "multipart/form-data, boundary=" + bound;
}
String MultiHTTP::GetBoundary(){
return bound;
}
void MultiHTTP::AddData(String name, String content){
req += "Content-Disposition: form-data; name=\"" + name +"\"\n\n";
req += content + "\n" + bound + "\n";
}
void MultiHTTP::AddFile(String name, String filename, String mime, String content){
req += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\n";
req += "Content-Type: " + mime + "\n";
req += "Content-Transfer-Encoding: binary\n\n";
req += content + "\n" + bound + "\n";
}
String MultiHTTP::GetRequest(){
return req;
}
And my Code is
MultiHTTP request;
request.AddData("username", "USER");
request.AddData("password", "PASSWORD");
request.AddData("upload_session", session[0]);
request.AddData("chunk_no", "1");
request.AddData("chunk_number", "1");
request.AddData("filesize", AsString(Filesize));
request.AddData("finalize", "1");
request.AddFile("fn", file, "text/plain", LoadFile(file));
LOG(request.GetRequest());
HttpRequest req(session[1]);
req.POST();
req.ContentType(request.GetContentType());
req.PostData(request.GetRequest());
ret = req.Execute();
if(!req.IsSuccess()){
PromptOK("FAILED");
PromptOK(req.GetErrorDesc());
return;
}
LOG(ret);
My class created this POST data
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="username"
USERNAME
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="password"
PASSWORD
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="upload_session"
O1QQRHKMDIDI
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_no"
1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_number"
1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="filesize"
69
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="finalize"
1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Sonic Youth
Deftones
Death From Above 1979
(Andrew Jackson Jihad)
---MULTIPART-BOUNDARY---657443953---2000568409---
But the Request fails. i only got this error
socket(6) / recv: Connection reset by peer
Whats wrong?
[Updated on: Sat, 30 March 2013 22:19] Report message to a moderator
|
|
|
Re: HttpRequest File Upload [message #39574 is a reply to message #39573] |
Sun, 31 March 2013 11:48   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
Brat wrote on Sat, 30 March 2013 16:11 | Okay,
i have to calculate the new Content-Length - how can i do this? strlen()? And how can i manipulate the Content-Length field?
#EDIT:
Okay, i created this class.
class MultiHTTP {
public:
MultiHTTP();
MultiHTTP(String boundary);
String GetContentType();
String GetBoundary();
void AddData(String name, String content);
void AddFile(String name, String filename, String mime, String content);
String GetRequest();
private:
String bound;
String req;
int64 Length;
};
MultiHTTP::MultiHTTP(){
bound = "---MULTIPART-BOUNDARY---" + AsString(Random()) + "---" + AsString(Random()) + "---";
req = bound + "\n";
}
MultiHTTP::MultiHTTP(String boundary){
bound = boundary;
req = bound + "\n";
}
String MultiHTTP::GetContentType(){
return "multipart/form-data, boundary=" + bound;
}
String MultiHTTP::GetBoundary(){
return bound;
}
void MultiHTTP::AddData(String name, String content){
req += "Content-Disposition: form-data; name=\"" + name +"\"\n\n";
req += content + "\n" + bound + "\n";
}
void MultiHTTP::AddFile(String name, String filename, String mime, String content){
req += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\n";
req += "Content-Type: " + mime + "\n";
req += "Content-Transfer-Encoding: binary\n\n";
req += content + "\n" + bound + "\n";
}
String MultiHTTP::GetRequest(){
return req;
}
And my Code is
MultiHTTP request;
request.AddData("username", "USER");
request.AddData("password", "PASSWORD");
request.AddData("upload_session", session[0]);
request.AddData("chunk_no", "1");
request.AddData("chunk_number", "1");
request.AddData("filesize", AsString(Filesize));
request.AddData("finalize", "1");
request.AddFile("fn", file, "text/plain", LoadFile(file));
LOG(request.GetRequest());
HttpRequest req(session[1]);
req.POST();
req.ContentType(request.GetContentType());
req.PostData(request.GetRequest());
ret = req.Execute();
if(!req.IsSuccess()){
PromptOK("FAILED");
PromptOK(req.GetErrorDesc());
return;
}
LOG(ret);
My class created this POST data
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="username"
USERNAME
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="password"
PASSWORD
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="upload_session"
O1QQRHKMDIDI
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_no"
1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_number"
1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="filesize"
69
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="finalize"
1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Sonic Youth
Deftones
Death From Above 1979
(Andrew Jackson Jihad)
---MULTIPART-BOUNDARY---657443953---2000568409---
But the Request fails. i only got this error
socket(6) / recv: Connection reset by peer
Whats wrong?
|
Hard to say. Try HttpRequest::SetTrace.
Mirek
|
|
|
Re: HttpRequest File Upload [message #39577 is a reply to message #39574] |
Sun, 31 March 2013 20:41   |
Brat
Messages: 7 Registered: March 2013
|
Promising Member |
|
|
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"
USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"
PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"
V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"
69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Sonic Youth
Deftones
Death From Above 1979
(Andrew Jackson Jihad)
---MULTIPART-BOUNDARY---2965041788---1648970112---
HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
HTTP retry on error socket(6) / recv: Connection reset by peer
Starting status 1 'Start', url: dlw191-2.share-online.biz
HTTP Execute: Start
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"
USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"
PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"
V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"
69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Sonic Youth
Deftones
Death From Above 1979
(Andrew Jackson Jihad)
---MULTIPART-BOUNDARY---2965041788---1648970112---
HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
HTTP retry on error socket(6) / recv: Connection reset by peer
Starting status 1 'Start', url: dlw191-2.share-online.biz
HTTP Execute: Start
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"
USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"
PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"
V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"
69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Sonic Youth
Deftones
Death From Above 1979
(Andrew Jackson Jihad)
---MULTIPART-BOUNDARY---2965041788---1648970112---
HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
HTTP retry on error socket(6) / recv: Connection reset by peer
Starting status 1 'Start', url: dlw191-2.share-online.biz
HTTP Execute: Start
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"
USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"
PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"
V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"
69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"
1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
Sonic Youth
Deftones
Death From Above 1979
(Andrew Jackson Jihad)
---MULTIPART-BOUNDARY---2965041788---1648970112---
HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
|
|
|
|
|
|
|
Re: HttpRequest File Upload [message #40963 is a reply to message #40962] |
Mon, 14 October 2013 16:47   |
Zbych
Messages: 327 Registered: July 2009
|
Senior Member |
|
|
Server response looks like this:
Content-Length: 111970
Content-MD5: c0c1946bc49372111b25c3aafe7327b1
Content-Type: multipart/related; boundary=blablabla
Date: Mon, 14 Oct 2013 13:52:06 GMT
Server: ServerName/1.0
Content-Type: application/json, charset=UTF-8
Content-ID: body
Some Json stuff here
--blablabla
Content-Type: application/octet-stream
Content-ID: attachment
Some binary data here
--blablabla--
Can you point me to some functions that I can extract from skylark, to parse this response? I guess that ReadMultiPart is the function that I am looking for.
[Updated on: Mon, 14 October 2013 16:52] Report message to a moderator
|
|
|
|
Re: HttpRequest File Upload [message #43117 is a reply to message #40184] |
Thu, 08 May 2014 14:35   |
 |
forlano
Messages: 1207 Registered: March 2006 Location: Italy
|
Senior Contributor |
|
|
Hello,
I am resuming this old post.
I am having problem with the upload file.
This a working PHP file (tested by browser via a html file driver) located in /var/www/vega/logup
<?php
//transfer.php
function Upload($folder)
{ $target = "./". basename( $_FILES['file']['name']) ;
echo $target;
if(copy($_FILES['file']['tmp_name'], $target)) return 1;
else return 0;
}
Upload("");
?>
It saves the file in the same folder. This is the U++ driver program:
void PublishSiteDlg::PublishVega()
{ String fn = TD.PATHDIRWWW+"compress.zip";
if (FileExists(fn)) DeleteFile(fn);
FileZip zip(fn);
String targetdir = TD.PATHDIRWWW;
FindFile fff(AppendFileName(targetdir, "*.*"));
while(fff) {
if(fff.IsFile()) {
zip.WriteFile(LoadFile(TD.PATHDIRWWW+fff.GetName()), fff.GetName());
}
fff.Next();
}
zip.Finish(); //all OK here and the file is created
HttpRequest req("http://localhost/vega/logup/transfer.php");
req.POST();
req.Part("file", LoadFile(fn), "application/zip", fn);
req.Post("submit", "submit");
String s = req.Execute();
Exclamation(s);
}
The previous code shows the echoed file name but no file is uploaded.
What I am missing?
Thanks,
Luigi
|
|
|
|
Goto Forum:
Current Time: Tue Apr 29 21:59:53 CEST 2025
Total time taken to generate the page: 0.00817 seconds
|
|
|