我被困在如何将python代码转换为c#。我尝试了很多次但没有收到API响应。首先我尝试了这个
我的python代码
import sys
import base64
import requests
import json
file_path = 'limit.jpg'
image_uri = "data:image/jpg;base64," + base64.b64encode(open(file_path, "rb").read()).decode()
r = requests.post("https://api",
data=json.dumps({'src': image_uri}),
headers={"app_id": "YOUR_APP_ID", "app_key": "YOUR_APP_KEY",
"Content-type": "application/json"})
print(json.dumps(json.loads(r.text), indent=4, sort_keys=True))
第一次尝试
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api."))
{
request.Headers.TryAddWithoutValidation("app_id", id);
request.Headers.TryAddWithoutValidation("app_key",apiKey);
request.Content = new StringContent("{ \"src\": \"data:image/jpeg;base64",PathToImage);
request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
var response = httpClient.SendAsync(request).Result;
Console.WriteLine( await response.Content.ReadAsStringAsync());
}
}
但是这里没有运气 API 响应是 {"error":"SyntaxError: Unexpected end of JSON input","error_info":{"id":"json_syntax","message":"SyntaxError: Unexpected end of JSON input"}} 我也尝试转换 cURL -
curl -X POST https://api \
-H 'app_id: YOUR_APP_ID' \
-H 'app_key: YOUR_APP_KEY' \
-H 'Content-Type: application/json' \
--data '{ "src": "data:image/jpeg;base64,'$(base64 -i limit.jpg)'" }'
但又没有运气了。也许我对图像中的 convertbase64 有误。
神不在的星期二
慕仙森
波斯汪
随时随地看视频慕课网APP
相关分类