我在Mediafire developers部分阅读了很多内容,但似乎没有为C#开发人员提供库,所以我尝试使用他们的Web服务API的
上传文件的正常方案如下:
1-通过此链接申请session_token
https://www.mediafire.com/api/user/get_session_token.php?email={0}&password={1}&application_id={2}&signature={3}&token_version=2
这一步工作正常,我得到了session_token
之后,您在帖子请求中使用session_token
通过此链接上传文件
http://www.mediafire.com/api/unversioned/upload/simple.php?uploadkey={0}&session_token={1}
Mediafire文档描述Raw HTTP Request上传:
POST http://www.mediafire.com/api/unversioned/upload/simple.php?uploadkey=5bb66g94blnnk&session_token=aa22f5a968f827daf69fd6b3515110c43e036bc5d2ed8b81657dd1bdfe4b4c3e3ea6757d1f47bc3d6a001a16bc6f25abb486c5e779328a5769bd9ed6064edb69 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
X-Filehash:564dc5e9541a494e966066da8b2392e2e70e2438e4fcf4b0058cd9249abc4e1d
X-Filesize:29278
X-Filetype:text/plain
Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 29278
-----------------------------41184676334
Summer vacation
-----------------------------41184676334
Content-Disposition: form-data; name="image1"; filename="GrandCanyon.jpg"
Content-Type: image/jpeg
(Binary data not shown)
-----------------------------41184676334--
我尝试HttpWebRequest
,但它不起作用
byte[] postData = File.ReadAllBytes(@"img2.jpg");
System.Net.ServicePointManager.Expect100Continue = false;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create("http://www.mediafire.com/api/1.1/upload/upload.php?uploadkey=3kh445&session_token=" + respon.session_token);
request.Method = "POST";
request.Headers.Clear();
request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0";
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");
request.ContentType = "multipart/form-data";
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
此代码抛出异常:
The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF
我也尝试了这个代码,并且保持请求没有响应:
string path = String.Format("http://www.mediafire.com/api/unversioned/upload/simple.php?uploadkey=3kh5656&session_token={0}", respon.session_token);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string http = string.Format(
@"
POST {0} HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
X-Filehash:{1}
X-Filesize:{2}
X-Filetype:text/plain
Content-Type: multipart/form-data; boundary=---------------------------{6}
Content-Length: {3}
-----------------------------{6}
Summer vacation
-----------------------------{6}
Content-Disposition: form-data; name=""image1""; filename=""{4}""
Content-Type: image/jpeg
{5}
-----------------------------{6}--"
,
path,
GetFileHash(@"img2.jpg"),
file.Length,
file.LongLength,
filename,
Encoding.Default.GetString(file),
DateTime.Now.Ticks.ToString("x")
);
sock.Connect(new IPAddress(Dns.GetHostEntry("www.mediafire.com").AddressList[0].GetAddressBytes()), 80);
sock.Send(Encoding.Default.GetBytes(http));
while (sock.Available == 0)
Thread.Sleep(1);
file = new byte[sock.Available];
sock.Receive(file, 0, sock.Available, SocketFlags.None);
string result = Encoding.ASCII.GetString(file);
sock.Close();
对不起英语不好有什么不清楚请告诉我我会编辑问题。
答案 0 :(得分:0)
我确实看到了一些错误。看起来MediaFire的文档需要更多更新。
在您的第一个示例中,您使用的网址是http://www.mediafire.com/api/1.1/upload/upload.php?uploadkey=3kh445&session_token=。我想指出一些事情:
我发现MediaFire文档存在错误,让您感到困惑。因此,您的网址应该更贴近这一点:http://www.mediafire.com/api/1.1/upload/simple.php?session_token=
需要注意的另一件事是,只有文件的二进制数据应该在帖子正文中。即使请求类型是POST,对于MediaFire上传API,任何其他参数也将在URL查询字符串中(除非参数在Header Data下)。因此,session_token,signature,filedrop_key,path,action_on_duplicate和mtime将始终位于URL的查询字符串部分。
我在您的XMLHTTPRequest示例中看到您添加了Content-Type = multipart / form-data标头,但是缺少边界信息。您发布的第二个未返回响应的示例具有正确形成的Content-Type +边界,但看起来您将其余参数放入POST主体而不是URL查询字符串。
最后一个例子看起来也有一个格式错误的网址。如果您要调用传统版本的MediaFire API,只需使用www.mediafire.com/api/api category / api name .php,例如www.mediafire。 COM / API /上传/ simple.php。如果您需要调用版本化API,则需要将版本添加到URL www.mediafire.com/api/version number / api category / api name .php,例如www.mediafire.com/api/1.1/upload/simple.php。
答案 1 :(得分:0)
MediaFire最近在Github上发布了一个C#SDK:
https://github.com/MediaFire/mediafire-csharp-open-sdk
在Visual Studio中,您也可以通过键入以下命令在程序包管理器控制台上使用NuGet进行安装:
Install-Package MediaFireSDK
在GitHub自述文件中,您将找到如何上传文件的示例。