Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 上传至imgur.com_C#_Httpwebrequest - Fatal编程技术网

C# 上传至imgur.com

C# 上传至imgur.com,c#,httpwebrequest,C#,Httpwebrequest,是一个图片上传网站,提供 我的代码看起来与他们作为示例提供的PHP代码一模一样。然而,在php代码中,它们是http\u build\u query($pvars) 他们似乎在发布查询之前对其进行URL编码。编辑:请注意,我已更改为完整的.NET 3.5,而不是客户端配置文件。这使我能够访问system.web,因此我使用了httputliity.urlencode()。这使得api返回一个“失败”和一个“未发送图像”。如果我没有编码,那么API会返回一个带有图片链接的“OK”,但是不会上传任何

是一个图片上传网站,提供

我的代码看起来与他们作为示例提供的PHP代码一模一样。然而,在php代码中,它们是http\u build\u query($pvars)

他们似乎在发布查询之前对其进行URL编码。编辑:请注意,我已更改为完整的.NET 3.5,而不是客户端配置文件。这使我能够访问
system.web
,因此我使用了
httputliity.urlencode()
。这使得api返回一个“失败”和一个“未发送图像”。如果我没有编码,那么API会返回一个带有图片链接的“OK”,但是不会上传任何图片(比如空白文件)

我如何修复我的代码,使其在API上正常工作

 Image image = Image.FromFile("C:\\Users\\Affan\\Pictures\\1509310.jpg");
        MemoryStream ms = new MemoryStream();
        // Convert Image to byte[]
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        byte[] imageBytes = ms.ToArray();

        WebRequest wb = WebRequest.Create(new Uri("http://imgur.com/api/upload.xml"));
        wb.ContentType = "application/x-www-form-urlencoded";            
        wb.Method = "POST";
        wb.Timeout = 10000;
        Console.WriteLine(imageBytes.Length);
        string parameters = "key=433a1bf4743dd8d7845629b95b5ca1b4&image=" + Convert.ToBase64String(imageBytes);


        Console.WriteLine("parameters: " + parameters.Length);
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        byte[] bytes = encoding.GetBytes(parameters);
        // byte[] bytes = Convert.FromBase64String(parameters);

        System.IO.Stream os = null;
        try { // send the Post
            wb.ContentLength = bytes.Length;   //Count bytes to send
            os = wb.GetRequestStream();               
            os.Write(bytes, 0, bytes.Length);         //Send it
        } catch (WebException ex) {
            MessageBox.Show(ex.Message, "HttpPost: Request error");
            Console.WriteLine(ex.Message);
        } finally {
            if (os != null) {
               // os.Close();
            }
        }

        try { // get the response
            WebResponse webResponse = wb.GetResponse();

            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            //MessageBox.Show(sr.ReadToEnd().Trim());
            Console.WriteLine(sr.ReadToEnd().Trim());
        } catch (WebException ex) {
            MessageBox.Show(ex.Message, "HttpPost: Response error");
        }       

我刚刚上传了这张图片

使用此代码:

using (var w = new WebClient())
{
    var values = new NameValueCollection
    {
        { "key", "433a1bf4743dd8d7845629b95b5ca1b4" },
        { "image", Convert.ToBase64String(File.ReadAllBytes(@"hello.png")) }
    };

    byte[] response = w.UploadValues("http://imgur.com/api/upload.xml", values);

    Console.WriteLine(XDocument.Load(new MemoryStream(response)));
}
您现在可能需要更改API密钥:-)

结果是:

<rsp stat="ok">
  <image_hash>IWg2O</image_hash>
  <delete_hash>fQAXiR2Fdq</delete_hash>
  <original_image>http://i.imgur.com/IWg2O.png</original_image>
  <large_thumbnail>http://i.imgur.com/IWg2Ol.jpg</large_thumbnail>
  <small_thumbnail>http://i.imgur.com/IWg2Os.jpg</small_thumbnail>
  <imgur_page>http://imgur.com/IWg2O</imgur_page>
  <delete_page>http://imgur.com/delete/fQAXiR2Fdq</delete_page>
</rsp>

IWg2O
fQAXiR2Fdq
http://i.imgur.com/IWg2O.png
http://i.imgur.com/IWg2Ol.jpg
http://i.imgur.com/IWg2Os.jpg
http://imgur.com/IWg2O
http://imgur.com/delete/fQAXiR2Fdq

以下是dtb针对v3 API的答案的更新版本,使用匿名上传(您需要在注册应用程序以获取客户端ID):

使用(var w=new WebClient())
{
字符串clientID=“”;
w、 添加(“授权”、“客户端ID”+客户端ID);
var values=新的NameValueCollection
{
{“image”,Convert.ToBase64String(File.ReadAllBytes(@“hello.png”)}
};
字节[]响应=w.上传值(“https://api.imgur.com/3/upload.xml“、价值观);
Console.WriteLine(XDocument.Load(newmemoryStream(response));
}
现在的回答是这样的(参见):


SbBGk
1341533193
图像/jpeg
假的
2559
1439
521916
1.
521916
EYZD3NJHSBRED1
http://i.imgur.com/SbBGk.jpg
我想这个解决方案已经过时了

    using (var w = new WebClient())
    {
        var values = new NameValueCollection
    {
        {"image", Convert.ToBase64String(imageData)},
        {"type", "base64"}
    };

        w.Headers.Add("Authorization", "Client-ID xxxxxxxxx");
       var response = w.UploadValues("https://api.imgur.com/3/image", values);
    }
另一种方法是:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image");
        request.Headers.Add("Authorization", "Client-ID xxxxxxx");
        request.Method = "POST";

        ASCIIEncoding enc = new ASCIIEncoding();
        string postData = Convert.ToBase64String(imageData);
        byte[] bytes = enc.GetBytes(postData);

        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = bytes.Length;

        Stream writer = request.GetRequestStream();
        writer.Write(bytes, 0, bytes.Length);

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

哇,我花了将近5个小时在那上面。。谢谢你。你能找出为什么我的方法行不通吗?回答得好!我一直喜欢“少即是多”的解决方案。诚然,你在没有错误检查的情况下只进行了简单的检查,但即使将其添加回。。。是的,不错。请注意,imgur已更改为其API的新版本。的URI是
http://api.imgur.com/2/upload
。响应XML已更改,但发出请求的C#代码与URI的区别相同。他们现在将其更改为v3。现在它似乎需要OpenAuth。您能否更新您的答案以反映这一点?->请不要将应用程序的密钥和id与代码放在一起。确认从2018年10月9日起,此操作仍能正常工作。请从URL中删除.xml,然后使用Encoding.UTF8.GetString(响应)查看JSON输出
    using (var w = new WebClient())
    {
        var values = new NameValueCollection
    {
        {"image", Convert.ToBase64String(imageData)},
        {"type", "base64"}
    };

        w.Headers.Add("Authorization", "Client-ID xxxxxxxxx");
       var response = w.UploadValues("https://api.imgur.com/3/image", values);
    }
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image");
        request.Headers.Add("Authorization", "Client-ID xxxxxxx");
        request.Method = "POST";

        ASCIIEncoding enc = new ASCIIEncoding();
        string postData = Convert.ToBase64String(imageData);
        byte[] bytes = enc.GetBytes(postData);

        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = bytes.Length;

        Stream writer = request.GetRequestStream();
        writer.Write(bytes, 0, bytes.Length);

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();