C# 使用C在MediaFire中上载#

C# 使用C在MediaFire中上载#,c#,mediafire,C#,Mediafire,有人在MediaFire中编写代码吗? 接下来,您可以进行自己的会话(当然,使用用户名和密码)。我使用此代码通过Post方法上传和发送值 private static string url = "HTTPS://www.mediafire.com/api/1.3/upload/simple.php?session_token=ed007dc432d5081952c15c50a 3f5c4dade894927dbcb8c44a59c6aefag6bd1d293f90434bfa 7bcd13d

有人在MediaFire中编写代码吗? 接下来,您可以进行自己的会话(当然,使用用户名和密码)。我使用此代码通过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);
        }
不幸的是,我看到了这个错误

“会话令牌错误。服务器提交了一个协议 违反。节=响应STATUSLINE“


任何帮助都将不胜感激。关于

那么,根据错误判断,session\u令牌值似乎是错误的。我现在也在使用Mediafire API,我能够幸运地处理大多数API调用(除了上传…) 因此,如果您阅读了文档,您知道第一步是创建第一个调用,在该调用中生成一个生存时间为10分钟的会话\u令牌(之后必须续订)。 要检索这些值,这里是我的代码

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


        }
成功创建会话_令牌后,您现在可以执行其他API调用。我注意到您的代码中缺少签名,但是使用token_version=2的任何(我认为是这样的,或者至少大多数)API调用都需要签名 计算签名有几个方面。这样做的方式取决于您是否在当前会话中进行了第一次呼叫。我输入了这个函数
sk->secret\u密钥
URI->可以像/api/1.4/upload/simple.php?session\u token=“+session\u 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();
    }
好的,现在确保你的代码一切正常。请求URL必须如下所示 “+session_token+”&signature=“+signature”

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

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