通过C#中的imageshack API发布图片 [英] Posting image via imageshack API in C#
问题描述
所以。以下是文档参考 [ ^ ]
我尝试过这样的方法
public static void UploadImage(HttpPostedFileBase image)
{
var httpWReq =
(HttpWebRequest)WebRequest.Create( http://www.imageshack.us/upload_api.php);
httpWReq.Method = POST;
httpWReq.ContentType = multipart / form-data;
httpWReq.KeepAlive = true ;
var stream = httpWReq.GetRequestStream();
var encoding = new UTF8Encoding();
var postData = key = / *我的钥匙* /;
postData + = & fileupload =;
byte [] data = encoding.GetBytes(postData);
stream.Write(data, 0 ,data.Length);
var buffer = new byte [ 4096 ];
int bytesread;
while ((bytesread = image.InputStream.Read(buffer, 0 ,buffer.Length ))!= 0 )
{
stream.Write(buffer, 0 ,bytesread);
}
stream.Close();
var response =(HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream())。ReadToEnd ();
}
但它给出了错误你必须提供有效的密钥。问题是密钥100%有效。当我改为
httpWReq.ContentType = application / x-www-form-urlencoded;
密钥不再出错,但有错误没有上传文件或空文件。
这里是一些示例代码,我如何使用这种方法
[HttpPost]
public ActionResult SomeAction(SomeViewModel model)
{
// I拍摄用户上传的图像并将其存储到imageshack
UploadeImage(model.Image);
RedirectToAction( Index);
}
所以问题是:此代码中是否有任何错误,或者是否有通过此API发布图像的下降方法?
提前致谢!
这个帮我解决了问题
http://dandar3.blogspot.com/2010/06/imageshackus-image-upload-c-code.html [ ^ ]
Hi guys! I'm seriously struggling several days, so any help would be appreciable
So. Here is reference for documentation[^]
I've tried such approach
public static void UploadImage(HttpPostedFileBase image)
{
var httpWReq =
(HttpWebRequest)WebRequest.Create("http://www.imageshack.us/upload_api.php");
httpWReq.Method = "POST";
httpWReq.ContentType = "multipart/form-data";
httpWReq.KeepAlive = true;
var stream = httpWReq.GetRequestStream();
var encoding = new UTF8Encoding();
var postData = "key=/*my key*/";
postData += "&fileupload=";
byte[] data = encoding.GetBytes(postData);
stream.Write(data, 0, data.Length);
var buffer = new byte[4096];
int bytesread;
while ((bytesread = image.InputStream.Read(buffer, 0, buffer.Length)) != 0)
{
stream.Write(buffer, 0, bytesread);
}
stream.Close();
var response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
but it gives me error "you must provide valid secret key". The problem is that key is 100% valid. And when I change to
httpWReq.ContentType = "application/x-www-form-urlencoded";
there is no longer error with key, but there is error "no file or empty file was uploaded".
Here's some sample code, how I use this method
[HttpPost]
public ActionResult SomeAction (SomeViewModel model)
{
//I take image uploaded by user and store it into imageshack
UploadeImage(model.Image);
RedirectToAction("Index");
}
So the question is: Are there any mistakes in this code or is there any more descent approach to post image via this API?
Thanks in advance!
this one helped me with problem
http://dandar3.blogspot.com/2010/06/imageshackus-image-upload-c-code.html[^]
这篇关于通过C#中的imageshack API发布图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!