示例#1
0
        public static string MarkdownToHtml(string mdText)
        {
            string       body       = "";
            string       url        = "https://api.github.com/markdown";
            WebResponse  response   = null;
            StreamReader readStream = null;
            Stream       dataStream = null;

            try {
                HttpWebRequest request = CreateRequest(url);
                request.Method = "POST";
                string postData  = "{\"text\":\"" + YamlParser.EscapeSymbols(mdText) + "\",\"mode\":\"gfm\",\"context\":\"github/gollum\"}";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length;
                dataStream            = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);

                response = request.GetResponse();
                Stream receiveStream = response.GetResponseStream();
                readStream = new StreamReader(receiveStream, Encoding.UTF8);
                body       = readStream.ReadToEnd();
            } catch (WebException e) {
                HMS.LogError("Ошибка получения Markdown данных с GitHub: " + url);
                HMS.LogError("Сообщение: " + e.Message + " Status: " + e.Status.ToString());
                if (e.Response != null)
                {
                    string resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
                    HMS.LogError(resp);
                }
            } catch (Exception e) {
                HMS.LogError(e.ToString());
            } finally {
                if (response != null)
                {
                    dataStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (readStream != null)
                {
                    readStream.Close();
                }
            }
            return(body);
        }