[英]Posting base64 converted image data
What will be equilent of this python code in C#. C#中的此python代码等效于什么。 This opens image from file and converts into base64 encoded string and sends the POST request.
这将打开文件中的图像,并将其转换为base64编码的字符串并发送POST请求。 Please help.
请帮忙。
#!/usr/bin/python
import requests
import base64
import json
# Sample image file is available at http://plates.openalpr.com/ea7the.jpg
IMAGE_PATH = '/tmp/sample.jpg'
SECRET_KEY = 'sk_DEMODEMODEMODEMODEMODEMO'
with open(IMAGE_PATH, 'rb') as image_file:
img_base64 = base64.b64encode(image_file.read())
url = 'https://api.openalpr.com/v2/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s' % (SECRET_KEY)
r = requests.post(url, data = img_base64)
print(json.dumps(r.json(), indent=2))
Not tested, but enough for you to start from. 未经测试,但足以让您开始。
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace image
{
class Program
{
static async Task Main(string[] args)
{
var bytes = File.ReadAllBytes("/tmp/sample.jpg");
var base64 = Convert.ToBase64String(bytes);
var secretKey = "my_key";
var url = $"http://yoururl.com?my_key={secretKey}";
using(var client = new HttpClient())
{
var content = new StringContent(base64);
var response = await client.PostAsync(url, content);
var stringResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine(stringResponse);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.