Package Microsoft Oxford Program API Client
The Oxford Program API is made up of a base URL, service name, parameter group service, mostly post (I haven't finished reading it yet), many of which are strings, but also have streaming formatting (like uploading pictures or something), Our projectoxfordclienthelper is to plan to encapsulate the implementation of the Oxford API to provide services to our different apicontroller.
Let's start by defining some basic fields
Private Const string ServiceHost = "https://api.projectoxford.ai/face/v1.0";
Private Const string KEY = "";
Private httpclient client;
private static string Photofolder = system.configuration.configurationmanager.appsettings["Projectoxfordphotos"];
ServiceHost is Oxford's Api,key you can get it by registering for the Oxford Development Program, Photofolder is where we need to upload the map.
We initialize httpclient in the constructor and add two required header identities for httpclient.
Public Projectoxfordclienthelper ()
{
client = new HttpClient ();
var querystring = httputility.parsequerystring (String. Empty);
Client. Defaultrequestheaders.add ("ContentType", "Application/json");
Client. Defaultrequestheaders.add ("Ocp-apim-subscription-key", Key);
}
Next, we want to implement two kinds of post submission, one is to mention the AC parameter, one is to submit the string parameter
Post that implements the submit string parameter
Public Async task<projectoxfordresponsemodels> Postasync (string Querkey, Object body, dictionary<string, String> querystr = null)
{
var querystring = httputility.parsequerystring (String. Empty);
if (querystr!= null)
{
foreach (var entry in querystr)
{
querystring[entry. Key] = entry. Value;
}
}
var uri = string. Format ("{0}/{1}?{ 2} ", ServiceHost, Querkey, querystring);
var jsonstr = Newtonsoft.Json.JsonConvert.SerializeObject (body);
byte[] Bytedata = Encoding.UTF8.GetBytes (JSONSTR);
Httpresponsemessage response;
using (var content = new Bytearraycontent (bytedata))
{
content. Headers.contenttype = new Mediatypeheadervalue ("Application/json");
Response = await client. Postasync (URI, content);
var msg = await response. Content.readasstringasync ();
Return to New Projectoxfordresponsemodels (MSG, Response. StatusCode);
}
The so-called string parameter is to serialize the object that implements the fields in JSON format and post it to the Oxford API.
var jsonstr = Newtonsoft.Json.JsonConvert.SerializeObject (body);
byte[] Bytedata = Encoding.UTF8.GetBytes (JSONSTR);
So remember that content types are defined as
Content. Headers.contenttype = new Mediatypeheadervalue ("Application/json");
It's like a picture. These streaming files do not use this method, so we overload a method
Public Async task<projectoxfordresponsemodels> Postasync (string querkey, byte[) body, dictionary<string , string> querystr = null) {var querystring = httputility.parsequerystring (String.
Empty); if (querystr!= null) {foreach (var entry in querystr) {querystring[entry. Key] = entry.
Value; }} var uri = string. Format ("{0}/{1}?{
2} ", ServiceHost, Querkey, querystring);
Httpresponsemessage response; using (var content = new Bytearraycontent (body)) {content.
Headers.contenttype = new Mediatypeheadervalue ("Application/octet-stream"); Response = await client.
Postasync (URI, content); var msg = await response.
Content.readasstringasync (); Return to New Projectoxfordresponsemodels (MSG, Response.
StatusCode); }
}
Look at the parameters, stream format content needs to be passed in byte array, but the actual processing is not much different, if the byte array is passed directly processing, or first serialized as a byte array, but to note that streaming media JSON encoding is different, Created With Raphaël 2.1.0 start is byte array? Byetarray to the Content is byte array? Application/octet-stream End Application/json JSON serialzeobj getBytes Yes No Yes
We'll provide a way to help deal with the Oxford API return value.
Public httpresponsemessage Createhttpresponsemessage (httprequestmessage request, projectoxfordresponsemodels result )
{
if (result). StatusCode = = Httpstatuscode.ok)
{return
request. Createresponse (Httpstatuscode.ok, result. message);
}
else
{return
request. Createerrorresponse (result. StatusCode, result. message);
}