繁体   English   中英

HTTP Post返回整个网站html,而不是JSON响应

[英]HTTP Post returns the whole website html instead of a JSON response

因此,我尝试使用 api从服务器获取JSON响应问题是它返回网站首页的html代码。 如果您查看api页面,它说它应该返回一些json。 我认为我的代码有问题。

有什么建议么?

图片即时通讯使用: 动漫图片

我的代码:

        static void Main(string[] args)
    {
        Image img = Image.FromFile("image.jpg");
        String base64 = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);

        var request = (HttpWebRequest)WebRequest.Create("http://www.whatanime.ga/api/search?token=<token>");

        var postData = base64;
        var data = Encoding.UTF8.GetBytes(postData);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

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

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

        Console.WriteLine("data:" + responseString);
        Console.ReadLine();
    }

    public static string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }

好吧,在弄乱了其他选项之后,我发现了一个可行的(对我来说)解决方案,我将其发布以帮助将来遇到相同问题的人们。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using System.Drawing;
using System.Net;
using System.Collections.Specialized;

namespace ConsoleApplication3
{
class Program
{
    static void Main(string[] args)
    {
        Image img = Image.FromFile("image.jpg");
        String base64 = ImageToBase64(img, System.Drawing.Imaging.ImageFormat.Jpeg);

        using (var client = new WebClient())
        {
            var values = new NameValueCollection();
            values["image"] = base64;
            var response = client.UploadValues("https://whatanime.ga/api/search?token=<token>", values);
            var responseString = Encoding.Default.GetString(response);
            Console.WriteLine("data: " + responseString);
            Console.ReadLine();
        }
    }

    public static string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, format);
            byte[] imageBytes = ms.ToArray();

            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }
    }
}

正如我在文档中阅读的

POST /api/search?token={your_api_token} HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Host: whatanime.ga

image={Base64 Encoded Image}

您必须提供密钥对值才能提供图像

像这样的东西

using (WebClient client = new WebClient())
{
    response = client.UploadValues("http://www.whatanime.ga/api/search?token=<token>", 
        new NameValueCollection() {{ "image", base64 }
    });
}

结束读取响应状态

Returns HTTP 403 if API token is invalid.

Returns HTTP 401 if API token is missing.   

Note that there is a hard limit of 1MB post size. You should ensure your Base64 encoded image is < 1MB. Otherwise the server responds with HTTP 413 (Request Entity Too Large).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM