Answered by:
Uploading files (MediaFire API)

Question
-
Hello community. I am trying to explore the Mediafire API and so far so good, except I am not being successful when uploading files. I have never done something similar before, so I have done some search on the web and I have seen some upload examples. Maybe so that you can help me, you will have to read the Core API documentation, but I think the Upload section is enough. As I am uploading a simple text file, with less than 4MB, I am using Upload Simple
This is my code so far
if (first_call) //TODO //when you are doing the first API call with the current session token, the way you create a signature is different than future API calls { byte[] postData = File.ReadAllBytes(@"C:\test.txt"); System.Net.ServicePointManager.Expect100Continue = false; System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create("http://www.mediafire.com/api/1.4/upload/simple.php?session_token=" + session_token + "&response_format=xml&signature=" + get_signature(secret_key, time, "/api/1.4/upload/simple.php?session_token=" + session_token , first_call)); request.Method = "POST"; // request.Headers.Clear(); //updated User Agent request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"; //OK request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5"); request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); // var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo); request.ContentType = "multipart/form-data; boundary=" + boundary; // request.ContentLength = postData.Length; //TODO //In the example above, the custom header x - filehash is supplied. While this header is not required for all upload scenarios, its inclusion is strongly recommended as it will ensure a higher level of upload data integrity.Although it is not shown in this example, the custom headers x - filename and x - filesize are also supported.The x - filesize header is required when Content - Type is multipart / form - data or application / octet - stream.The x - filename header is required when Content - Type is application / octet - stream(this is because the filename is not included in the POST body like it is for multipart / form - data). Stream requestStream = request.GetRequestStream(); requestStream.Write(postData, 0, postData.Length); requestStream.Flush(); //? requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); }
When I attempt to run this code, HttpWebResponse throws an exception the server committed a protocol violation. Section=ResponseStatusLine
What am I doing wrong? Now, you may wonder if I am doing the request properly. According with the documentation everything looks ok. I tried another API calls related with User information and everything was ok, so I wouldn't say signature or session_token values are wrong...
The Mediafire Core documention also talks about Headers.. How can I add them? [Anatomy of an Upload]
In the example above, the custom header x-filehash is supplied. While this header is not required for all upload scenarios, its inclusion is strongly recommended as it will ensure a higher level of upload data integrity. Although it is not shown in this example, the custom headers x-filename and x-filesize are also supported. The x-filesize header is required when Content-Type is multipart/form-data or application/octet-stream. The x-filename header is required when Content-Type is application/octet-stream(this is because the filename is not included in the POST body like it is for multipart/form-data)
Meu novo programa (Bloco de Notas) http://hyrokumata-app.blogspot.pt/2012/07/notepad-3-beta.html
- Moved by CoolDadTx Thursday, August 27, 2015 2:02 PM Wrong forums
Thursday, August 27, 2015 10:59 AM
Answers
-
These forums are for MS products and technologies. Questions related to the use and calling of third party products should be posted in their forums (https://www.mediafire.com/developers/).Thursday, August 27, 2015 2:01 PM