使用C#在MediaFire上传

时间:2015-06-15 09:13:22

标签: c# mediafire

有没有人在MediaFire中编写代码? 在link之后,您可以进行自己的会话(当然还有您的用户名和密码)。我使用此代码通过Post方法上传和发送值。

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);
        }

然后不幸的是我看到了这个错误,

  

“会话令牌错误。服务器提交了一个协议   violation.section = ResponseStatusLine”

非常感谢任何帮助。问候

1 个答案:

答案 0 :(得分:0)

好吧,根据Error看起来session_token值是错误的。我现在也在使用Mediafire API,我能够通过大多数API调用获得好运(除了上传...) 因此,如果您阅读文档,您知道第一步是创建第一个调用,在其中生成一个生命周期为10分钟的session_token(之后您必须续订)。 要检索这些值,这里是我的代码

    public int secret_key;
    public string session_token; //is a token used to authorize user specific transactions
    public string time;
    bool first_call = true;
    bool new_key = false;



        //Creating a 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 as 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;
            }


        }

成功创建session_token后,您现在可以执行其他API调用。我在你的代码中注意到的一点是缺少签名,但是对于使用token_version = 2的任何(我认为是这样,或者至少大部分)API调用都需要签名 要计算签名,有几个方面。这样做的方式取决于它是否是您当前会话的第一次通话。我输入了这个功能。
sk - > secret_key
URI - >可以像/api/1.4/upload/simple.php?session_token=" + session_token
first_call - >只是为了确定它是否是当前会话的第一次通话

   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 = the Message-Digest (MD5) of the 'secret_key' modulo 256 + 'time' + the URI of the 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 to hex format
        StringBuilder oSb = new StringBuilder();

        for (int x = 0; x < HashData.Length; x++)
        {
            //hexadecimal string value
            oSb.Append(HashData[x].ToString("x2"));
        }

        return oSb.ToString();
    }

您将看到函数neosecretkey()。这对于当前会话的第二次和将来的呼叫是必要的。

static public int neo_secretkey(int sk)
        {
            long n_secret = (Convert.ToInt64(sk) * 16807) % 2147483647;
            return Convert.ToInt32(n_secret);
        }

好了,现在确保您的代码一切正常。请求URL必须如下所示 &#34; http://www.mediafire.com/api/1.4/upload/simple.php?session_token=&#34; + session_token +&#34;&amp; signature =&#34; +签名

我的代码有点难看,我对C#没有太多经验。希望这可以帮助你:)