Exemplo n.º 1
0
        public static async Task <string> PostNewAsync(StorageFile image)
        {
            string     result;
            string     token    = AccessToken.getAccessToken();
            string     host     = BaiduApi.getUrl() + "?access_token=" + token;
            Encoding   encoding = Encoding.Default;
            HttpClient client   = new HttpClient();
            //client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");
            string imageString = await toBase64Async(image);

            //str += "&disp_formula=true";
            byte[] buffer = encoding.GetBytes(imageString);
            //client.DefaultRequestHeaders.Add("Content-Length", buffer.Length.ToString());
            //StringContent content = new StringContent(buffer);
            List <KeyValuePair <string, string> > pairs = new List <KeyValuePair <string, string> >();

            pairs.Add(new KeyValuePair <string, string>("image", imageString));
            pairs.Add(new KeyValuePair <string, string>("disp_formula", "true"));
            FormUrlEncodedContent content = new FormUrlEncodedContent(pairs);

            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
            var response = await client.PostAsync(host, content);

            //request.GetRequestStream().Write(buffer, 0, buffer.Length);
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //Stream stream = response.GetResponseStream();
            //StreamReader reader = new StreamReader(stream, Encoding.Default);
            //result = reader.ReadToEnd();
            result = await response.Content.ReadAsStringAsync();

            return(result);
        }
Exemplo n.º 2
0
        public static async Task <string> PostAsync(StorageFile image)
        {
            string result;
            //string token = "24.5e94ae528427824fb5e03aaf8751a7ac.2592000.1592472357.282335-19946172";
            string token = AccessToken.getAccessToken();
            string host  = BaiduApi.getUrl() + "?access_token=" + token;
            //string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/formula?access_token=" + token;
            Encoding       encoding = Encoding.Default;
            HttpWebRequest request  = (HttpWebRequest)WebRequest.Create(host);

            request.Method    = "post";
            request.KeepAlive = true;
            string base64 = await toBase64Async(image);

            String str = "image=" + base64;

            str += "&disp_formula=true";
            byte[] buffer = encoding.GetBytes(str);
            request.ContentType = "application/x-www-form-urlencoded";
            var handler = new HttpClientHandler();

            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream          stream   = response.GetResponseStream();
            StreamReader    reader   = new StreamReader(stream, Encoding.Default);

            result = reader.ReadToEnd();
            stream.Close();
            response.Close();
            return(result);
        }