Upload in MediaFire with C# -


is there wrote codes in mediafire? following link, can take own session (of course username , password). used code uploading , send values through post method.

private static string url = "https://www.mediafire.com/api/1.3/upload/simple.php?session_token=ed007dc432d5081952c15c50a  3f5c4dade894927dbcb8c44a59c6aefag6bd1d293f90434bfa  7bcd13d284069aabfa528623601a39b7026ca534acf21a6de1  0343543e271ac5a44ca&action_on_duplicate=&response_  format=json";         try         {             string posturl = url;             httpwebrequest request = (httpwebrequest)webrequest.create(posturl);             byte[] bytes;             bytes = file.readallbytes(@"g:\untitled.jpg");               request.headers.clear();             request.method = "post";             request.headers.add(httprequestheader.acceptlangua  ge, "en-us,en;q=0.8,fa;q=0.6");             request.headers.add(httprequestheader.acceptencodi  ng, "gzip, deflate");             request.contenttype = "multipart/form-data";             request.referer = @"https://www.mediafire.com/developers/tools/api_tools";             request.contentlength = bytes.length;                 stream requeststream = request.getrequeststream();             requeststream.write(bytes, 0, bytes.length);             requeststream.close();             httpwebresponse response;             response = (httpwebresponse)request.getresponse();             if (response.statuscode == httpstatuscode.ok)             {                 stream responsestream = response.getresponsestream();                 string responsestr = new streamreader(responsestream).readtoend();                 textbox1.text = responsestr;                // return responsestr;             }         }         catch (exception s)         {             messagebox.show("session token error. " + s.message);         } 

then unfortunately see error,

“session token error. server committed protocol violation.section=responsestatusline”

any appreciated. regards

well, according error looks session_token value wrong. working mediafire api right , able luck api calls (except upload...) so, if read documentation, know first step create first call generate session_token life time of 10 minutes (after must renew it). retrieve values, here code

    public int secret_key;     public string session_token; //is token used authorize user specific transactions     public string time;     bool first_call = true;     bool new_key = false;            //creating first session token mediafire          string email = "";         string password = "";         string app_id = "";         string app_key = "";          byte[] b = encoding.utf8.getbytes(email + password + app_id + app_key);         sha1 sh = new sha1managed();         byte[] b2 = sh.computehash(b);         string signature = bitconverter.tostring(b2).replace("-", "").tolower();         string req_url = string.format("https://www.mediafire.com/api/1.4/user/get_session_token.php?email={0}&password={1}&application_id={2}&signature={3}&token_version=2", email, password, app_id, signature);          httpwebrequest myreq = (httpwebrequest)webrequest.create(req_url);         try         {             webresponse response = myreq.getresponse();             stream responsestream = response.getresponsestream();              xelement root = xdocument.load(responsestream).root;              if (root.element("result").value == ("success"))             {                 secret_key = convert.toint32(root.element("secret_key").value);                 session_token = root.element("session_token").value;                 time = root.element("time").value;             }             else if (root.element("result").value == ("error"))             {                 //todo                 //check errors list on mediafire api                 int error = convert.toint32(root.element("error").value);                 string message_error = root.element("message").value;             }         }         catch (system.net.webexception webex)         {              httpwebresponse response = webex.response httpwebresponse;             if (response.statuscode == httpstatuscode.forbidden)             {                 var r_error = (httpwebresponse)webex.response;                 stream receivestream = r_error.getresponsestream();                 streamreader reader = new streamreader(receivestream, encoding.utf8);                 string s = reader.readtoend().tostring();                 xelement response_body = xdocument.parse(s).root;                 int error = convert.toint32(response_body.element("error").value);                 string message_error = response_body.element("message").value;             }             else if (response.statuscode != httpstatuscode.forbidden)             {                 throw;             }           } 

once session_token created success, able other api calls. noticed on code signature missing, required (i think so, or @ least of them) api call using token_version=2 calculate signatures there few aspects. way depend if first call current session or not. typed function.
sk -> secret_key
uri -> can /api/1.4/upload/simple.php?session_token=" + session_token
first_call -> determine if first call current session or not

   static public string get_signature(int sk, string time, string uri, bool first_call)     {         string input = "";         if (first_call)         {             sk %= 256;             input = sk + time + uri;         }         else         {             sk = neo_secretkey(sk);             sk %= 256;             input = sk + time + uri;         }          //signature = message-digest (md5) of 'secret_key' modulo 256 + 'time' + uri of api call.          byte[] bytedata = encoding.ascii.getbytes(input);         //md5 creating md5 object.         md5 omd5 = md5.create();         //hash          byte[] hashdata = omd5.computehash(bytedata);          //convert byte array hex format         stringbuilder osb = new stringbuilder();          (int x = 0; x < hashdata.length; x++)         {             //hexadecimal string value             osb.append(hashdata[x].tostring("x2"));         }          return osb.tostring();     } 

you see function neosecretkey(). necessary second , future calls current session.

static public int neo_secretkey(int sk)         {             long n_secret = (convert.toint64(sk) * 16807) % 2147483647;             return convert.toint32(n_secret);         } 

well, make sure ok code. request url has "http://www.mediafire.com/api/1.4/upload/simple.php?session_token=" + session_token + "&signature=" + signature

my code bit ugly, have not experience c#. hope helps :)


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -