Package Microsoft Oxford Program API client _api

Source: Internet
Author: User
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);
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.