简体   繁体   中英

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

So im trying to get a JSON response from a server using this api The problem is that it returns the html code of the homepage of the website. If you look at the api page it says that it should return some json. I think there's something wrong with my code.

Any suggestions?

The image im using: 动漫图片

My code:

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

Okay after some messing with other options I've found a working (for me) solution I'm posting this to help people in the future with the same problem.

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

As I read in documentation

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}

you have to provide key pairs value to provide image

something like this

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

end read response state

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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