public void simplePost()
{
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/ocr?language=en&detectOrientation=true&textAngle=90");
// httpWebRequest.Headers.Add("Host","westcentralus.api.cognitive.microsoft.com");
// getkey from microsoft
httpWebRequest.Headers.Add("Ocp-Apim-Subscription-Key", "Microsoftkey");
#if JSON
httpWebRequest.ContentType = "application/json";
#else
httpWebRequest.ContentType = "application/octet-stream";
#endif
httpWebRequest.Method = "POST";
var webStr= httpWebRequest.GetRequestStream();
using (var streamWriter = new StreamWriter(webStr))
{
string json = " {\"url\":\"https:/myweb.com/beam1.png\"}";
string localf = @"Y:\Documents\slapsp1.png";
#if JSON
streamWriter.Write(json);
#else
byte[] data = GetByteArrayOfImage(localf);
webStr.Write(data,0,data.Length);
webStr.Close();
#endif
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string result = streamReader.ReadToEnd();
textBox1.Text = result;
}
}
ทดลอง ล่าสุด ไม่ได้ ยังไม่รู้สาเหตุ
Code ของ Curl
curl -i -d "{\"url\": \"http://abc.com/x.jpg\"}" -H "Content-Type: application/json" -H "Prediction-Key: 1212324234123412341234" -X POST https://southcentralus.api.cognitive.microsoft.com/customvision/v3.0/Prediction/xxxx-aaaa-bbb-ccc/classify/iterations/Iteration3/url?application=CHK_ABC
Code ของ Python 3.8 ต้องลง rest ก่อน python -m pip install rest
import requests
headers = {
'Content-Type': 'application/json',
'Prediction-Key': 'key',
}
params = (
('application', 'CHK_ABC'),
)
data = '{"url": "http://abc.com/x.jpg"}'
response = requests.post('https://southcentralus.api.cognitive.microsoft.com/customvision/v3.0/Prediction/prjectid-xxxx/classify/iterations/Iteration3/url', headers=headers, params=params, data=data)
print(response.text)