Esempio n. 1
0
        public static String[] Microsoft_GetStrings(string[] strs, string from, string to)
        {
            string texts = "[";
            string split = null;

            foreach (string str in strs)
            {
                texts += split + "\"" + str.Replace(",", ",") + "\"";
                if (null == split)
                {
                    split = ",";
                }
            }
            texts += "]";
            string httpstr = _http.GetRequest(_url_Microsoft_TranslateList + string.Format("?appId={0}&texts={1}&from={2}&to={3}&maxTranslations=5", _url_Microsoft_v2_key, texts, from, to));

            /*
             * IList<String[]> resultList = HelperBase.GetString(httpstr, "TranslatedText\\W{3}([^,]*)\\W,\\WTranslatedTextSentenceLengths");
             * string[] result = new string[resultList.Count];
             * for (int i = 0, len = resultList.Count; i < len; i++)
             * {
             *  result[i] = resultList[i][0];
             * }
             * */
            string[] result = HelperBase.GetString(1, httpstr, "TranslatedText\\W{3}([^,]*)\\W,\\WTranslatedTextSentenceLengths");
            return(result);
        }
Esempio n. 2
0
        public static string Microsoft_Get(string str, string from, string to)
        {
            string httpstr = _http.GetRequest(_url_Microsoft_Translate + string.Format("?appId={0}&text={1}&from={2}&to={3}", _url_Microsoft_v2_key, str, from, to));

            httpstr = HelperBase.GetString(httpstr, "\\\"(.*)\\\"", 1);
            return(httpstr.Replace("\\u000d", "").Replace("\\u000a", ""));
        }
Esempio n. 3
0
        /// <summary>
        /// Post And Get
        /// </summary>
        /// <param name="targetUrl">URL</param>
        /// <param name="method">Method</param>
        /// <param name="accept">Accept</param>
        /// <param name="contentType">Content Type</param>
        /// <param name="userAgent">User Agent</param>
        /// <param name="encoding">Encoding</param>
        /// <param name="cookies">Cookies</param>
        /// <param name="strPostdata">Post Data</param>
        public string GetRequest(string targetUrl, string method, string accept, string contentType, string userAgent, Encoding encoding, CookieContainer cookies, string strPostdata)
        {
            this.SetRequest(targetUrl, method, accept, contentType, userAgent, encoding, cookies);
            if (null != strPostdata && methodType.POST == this.Method)
            {
                byte[] buffer = this.Encoding.GetBytes(strPostdata);
                this.Request.ContentLength = buffer.Length;
                this.Request.GetRequestStream().Write(buffer, 0, buffer.Length);
                this.Request.GetRequestStream().Close();
            }
            #region 得到请求的response
            string resultData = null;
            try
            {
                using (this.Response = (HttpWebResponse)this.Request.GetResponse())
                {
                    Stream ResposeStream = this.Response.GetResponseStream();
                    if (Encoding.Default == this.Encoding && this.AutoGetEncoding)
                    {
                        MemoryStream _stream     = HelperBase.CopyStream(ResposeStream);
                        byte[]       RawResponse = _stream.ToArray();
                        string       temp        = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);

                        string charset = HelperBase.GetString(temp, "<meta([^<]*)\\bcharset\\b\\s*=\\s*([^<';\"]*)[\"']", 2);
                        if (!string.IsNullOrEmpty(charset))
                        {
                            this.Encoding = Encoding.GetEncoding(charset);
                            resultData    = this.Encoding.GetString(RawResponse);
                        }
                        else
                        {
                            if (this.Response.CharacterSet.ToLower().Trim() == "iso-8859-1")
                            {
                                encoding = Encoding.Default;
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(this.Response.CharacterSet.Trim()))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(this.Response.CharacterSet);
                                }
                            }
                        }
                    }
                    else
                    {
                        string ContentEncoding = this.Response.ContentEncoding;
                        if (null != ContentEncoding && ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            using (this.Reader = new StreamReader(new GZipStream(ResposeStream, CompressionMode.Decompress), this.Encoding))
                            {
                                resultData = this.Reader.ReadToEnd();
                            }
                        }
                        else
                        {
                            using (this.Reader = new StreamReader(ResposeStream, this.Encoding))
                            {
                                resultData = this.Reader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError &&
                    ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        resultData = "Error:Not found";
                    }
                    else
                    {
                        resultData = "Error:Forbidden";
                    }
                }
                else
                {
                    resultData = "Error:Unknow";
                }
            }
            return(resultData);

            #endregion
        }