コード例 #1
0
ファイル: wabr.cs プロジェクト: Inlite/wabr-net
    internal static string _method  = "post"; // gen be "get", "post", "postenc"


    private string procWebException(WebException e, WACallback cb)
    {
        string err = "";

        using (WebResponse response = e.Response)
        {
            HttpWebResponse result = (HttpWebResponse)response;
            if (result != null)
            {
                err = "HttpError " + ((int)result.StatusCode).ToString() + ".  " + result.StatusDescription;
            }
            else
            {
                err = "ResponseError: " + e.Message;
            }
            cb.call("response", err);
            if (response != null)
            {
                // WAUtils.printLine("Error code: {0}", httpResponse.StatusCode);
                using (Stream data = response.GetResponseStream())
                    if (data != null)
                    {
                        using (var reader = new StreamReader(data))
                        {
                            string message = reader.ReadToEnd();
                            if (!message.StartsWith("<!DOCTYPE")) // Do not display HTML message
                            {
                                err += ".  " + message;
                            }
                        }
                    }
            }
        }
        return(err);
    }
コード例 #2
0
ファイル: wabr.cs プロジェクト: Inlite/wabr-net
    internal void ReadAsync(string image, delReaderCallback callback, Object obj, string types = "", string directions = "", uint tbr_code = 0)
    {
        _barcodes = new WABarcode[] { };
        _error    = "";

        WACallback cb = new WACallback();

        if (callback != null)
        {
            cb.callback = callback;
            cb.obj      = obj;
            cb.isAsync  = true;
        }
        else
        {
            cb.callback = _ReaderCallback;
            cb.obj      = this;
            cb.isAsync  = false;
        }
        _image = image;
        cb.call("image", image);

        string[]      names = image.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        List <string> urls = new List <string>(), files = new List <string>(), images = new List <string>();

        foreach (string name1 in names)
        {
            string name = name1.Trim();
            string s    = name.ToLower();
            if (s.StartsWith("http://") || s.StartsWith("https://") || s.StartsWith("ftp://") || s.StartsWith("file://"))
            {
                urls.Add(name);
            }
            else if (File.Exists(name))
            {
                files.Add(name);
            }
            else if (name.StartsWith("data:") || WAUtils.isBase64(name))
            {
                images.Add(name);
            }
            else
            {
                throw new Exception("Invalid image source: " + name.Substring(0, Math.Min(name.Length, 256)));
            }
        }



        ReadLocal(urls, files, images, types, directions, tbr_code, cb);
    }
コード例 #3
0
ファイル: wabr.cs プロジェクト: Inlite/wabr-net
 private string procWebResponse(HttpWebResponse result, WACallback cb)
 {
     try
     {
         cb.call("response", result);
         using (Stream stream = result.GetResponseStream())
         {
             using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
             {
                 string txtResponse = reader.ReadToEnd();
                 return(txtResponse);
             }
         }
     }
     finally
     {
         result.Close();
     }
 }
コード例 #4
0
ファイル: wabr.cs プロジェクト: Inlite/wabr-net
    internal void ExecRequest(string serverUrl, string auth, List <string> files, Dictionary <string, string> queries, WACallback cb = null)
    {
        try
        {
            switch (WAHttpRequest._method)
            {
            case "get":
            {
                string query = "";
                foreach (string key in queries.Keys)
                {
                    if (key != "image")
                    {
                        query += key + "=" + HttpUtility.UrlEncode(queries[key]) + "&";
                    }
                }
                performRequest(serverUrl + "?" + query, auth, "GET", "", null, cb, 0);
            }
            break;

            case "post":
            {
                string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
                byte[] formData         = GetMultipartFormData(queries, files, formDataBoundary);
                performRequest(serverUrl, auth, "POST", "multipart/form-data; boundary=" + formDataBoundary, formData, cb, 0);
            }
            break;

            case "postenc":
            {
                string query = "";
                foreach (string key in queries.Keys)
                {
                    query += key + "=" + HttpUtility.UrlEncode(queries[key]) +
                             "&";
                }
                byte[] data = Encoding.ASCII.GetBytes(query);
                performRequest(serverUrl, auth, "POST", "application/x-www-form-urlencoded", data, cb, 0);
            }
            break;

            default:
                string err = "Invalid HTTP method: " + WAHttpRequest._method;
                cb.call("error", err);
                break;
            }
        }
        catch (Exception ex)
        {
            cb.call("error", ex.Message);
        }
    }
コード例 #5
0
ファイル: wabr.cs プロジェクト: Inlite/wabr-net
    private async Task performRequest(string url, string auth, string method, string type, byte[] data, WACallback cb, int retries)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        if (auth == null)
        {
            auth = "";
        }
        if (auth == "")
        {
            string env = Environment.GetEnvironmentVariable("WABR_AUTH");
            if (env != null)
            {
                auth = env;
            }
        }

        request.Method = method;
        if (type != "")
        {
            request.ContentType = type;
        }
        if (auth != "")
        {
            request.Headers.Add("Authorization", auth);
        }
        try
        {
            if (_timeoutSec != 0) // default is 100sec
            {
                request.Timeout = _timeoutSec * 1000;
            }

            if (data != null)
            {
                try
                {
                    request.ContentLength = data.Length;
                    if (cb.isAsync)
                    {
                        using (Stream stream = await request.GetRequestStreamAsync())
                        {
                            stream.Write(data, 0, data.Length);
                            stream.Close();
                        }
                    }
                    else
                    {
                        using (Stream stream = request.GetRequestStream())
                        {
                            stream.Write(data, 0, data.Length);
                            stream.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    string err = "RequestError: " + ex.Message;
                    cb.call("error", err);
                }
            }
            HttpWebResponse result = null;
            if (cb.isAsync)
            {
                result = (HttpWebResponse)(await request.GetResponseAsync());
            }
            else
            {
                result = (HttpWebResponse)(request.GetResponse());
            }
            string      txtResponse = procWebResponse(result, cb);
            WABarcode[] barcodes    = WABarcodeReader.ParseResponse(txtResponse);
            cb.call("barcodes", barcodes);
        }
        catch (Exception ex)
        {
            WebException e = null;
            if (ex is WebException)
            {
                e = (WebException)ex;
            }
            else if (ex.InnerException is WebException)
            {
                e = (WebException)ex.InnerException;
            }
            string err = ""; // getResponse (request);
            if (e != null)
            {
                // Receive failure can be result of Server closing without notifying connection
                // e.g. if Server stopped than started or W3WP.exe was killed
                if (e.Status == WebExceptionStatus.KeepAliveFailure && retries < 2)
                {
                    performRequest(url, auth, method, type, data, cb, retries + 1);
                }
                else if (e.Response != null && e.Response.ResponseUri != null &&
                         e.Response.ResponseUri.ToString().ToLower() != url.ToLower() && retries < 3) // Possible redirect retry, e.g using http:// instead of https://
                {
                    performRequest(e.Response.ResponseUri.ToString(), auth, method, type, data, cb, retries + 1);
                }
                else
                {
                    err = procWebException(e, cb);
                }
            }
            else
            {
                err = "FAILURE: " + ex.Message + " {" + ex.GetType().ToString() + "}";
            }
            if (err != "")
            {
                cb.call("error", err);
            }
        }
    }
コード例 #6
0
ファイル: wabr.cs プロジェクト: Inlite/wabr-net
    void ReadLocal(List <string> urls, List <string> files, List <string> images, string types_, string dirs_, uint tbr_, WACallback cb)
    {
        string server = _serverUrl;

        if (server == "")
        {
            server = "https://wabr.inliteresearch.com"; // default server
        }
        Dictionary <string, string> queries = new Dictionary <string, string>();

        string url = "";

        foreach (string s in urls)
        {
            if (url != "")
            {
                url += "|";
            }
            url += s;
        }
        if ("url" != "")
        {
            queries.Add("url", url);
        }

        string image = "";

        foreach (string s in images)
        {
            if (image != "")
            {
                image += "|";
            }
            image += s;
        }
        if ("image" != "")
        {
            queries.Add("image", image);
        }

        queries.Add("format", "xml");
        queries.Add("fields", "meta");
        if (types_ != "")
        {
            queries.Add("types", types_);
        }
        if (dirs_ != "")
        {
            queries.Add("options", dirs_);
        }
        if (tbr_ != 0)
        {
            queries.Add("tbr", tbr_.ToString());
        }

        string        serverUrl = String.Format("{0}/barcodes", server);
        WAHttpRequest http      = new WAHttpRequest();

        http.ExecRequest(serverUrl, _authorization, files, queries, cb);
    }