Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » HttpRequest File Upload
Re: HttpRequest File Upload [message #39574 is a reply to message #39573] Sun, 31 March 2013 11:48 Go to previous messageGo to previous message
mirek is currently offline  mirek
Messages: 13980
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
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to use Skylark on external server
Next Topic: [SOLVED] Fix some memory leak in IpAddrInfo
Goto Forum:
  


Current Time: Thu May 16 09:39:17 CEST 2024

Total time taken to generate the page: 0.02848 seconds