本文整理汇总了C#中System.Text.Encoding.GetString方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetString方法的具体用法?C# Encoding.GetString怎么用?C# Encoding.GetString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.GetString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Get
/// <summary>
/// 获取远程信息
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="encoding">请求编码</param>
/// <param name="wc">客户端</param>
public static string Get(string url, Encoding encoding = null, WebClient wc = null)
{
if (string.IsNullOrWhiteSpace(url)) { return string.Empty; }
url = url.Replace("\\", "/");
if (encoding == null) encoding = Encoding.UTF8;
var isNew = wc == null;
if (wc == null)
{
wc = new WebClient();
wc.Proxy = null;
wc.Headers.Add("Accept", "*/*");
wc.Headers.Add("Referer", url);
wc.Headers.Add("Cookie", "bid=\"YObnALe98pw\";");
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.5 Safari/537.31");
}
string strResult = null;
try
{
var data = wc.DownloadData(url);
strResult = encoding.GetString(data);
}
catch { return string.Empty; }
finally
{
if (!isNew) Cookies(wc);
if (isNew) wc.Dispose();
}
return strResult;
}
示例2: ReadLineByLine
/// <summary>
/// Reads the file at the given path line by line.
/// </summary>
/// <param name="path"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static IEnumerable<string> ReadLineByLine(string path, Encoding encoding)
{
bool isFirstLine = true;
FileStream fs = new FileStream(path, FileMode.Open);
try
{
int b = 0;
List<byte> lineBuilder = new List<byte>(256);
while ((b = fs.ReadByte()) >= 0)
{
if (b == '\n')
{
if (isFirstLine)
{
yield return encoding.GetString(lineBuilder.ToArray()).Substring(1);
isFirstLine = false;
}
else
{
yield return encoding.GetString(lineBuilder.ToArray());
}
lineBuilder.Clear();
}
else if (b != '\r')
{
lineBuilder.Add((byte)b);
}
}
}
finally
{
fs.Close();
}
}
示例3: ReadStringInternalDynamic
internal static string ReadStringInternalDynamic(this Stream stream, Encoding encoding, char end)
{
int characterSize = encoding.GetByteCount("e");
Debug.Assert(characterSize == 1 || characterSize == 2 || characterSize == 4);
string characterEnd = end.ToString();
int i = 0;
byte[] data = new byte[128 * characterSize];
while (true)
{
if (i + characterSize > data.Length)
{
Array.Resize(ref data, data.Length + (128 * characterSize));
}
int read = stream.Read(data, i, characterSize);
Debug.Assert(read == characterSize);
if (encoding.GetString(data, i, characterSize) == characterEnd)
{
break;
}
i += characterSize;
}
if (i == 0)
{
return "";
}
return encoding.GetString(data, 0, i);
}
示例4: GetTextForChunked
public static string GetTextForChunked(byte[] bytes, int contentStartIndex, int contentLength, bool isGzip, Encoding coding)
{
string html = string.Empty;
Stream contentStream = new MemoryStream();
int lastEnterIndex = contentStartIndex;
for (int i = contentStartIndex; i < contentLength + contentStartIndex; i++)
{
if (bytes[i] == 13 && bytes[i + 1] == 10)
{
int chunkLength = int.Parse(coding.GetString(bytes, lastEnterIndex, i - lastEnterIndex).Trim(), System.Globalization.NumberStyles.AllowHexSpecifier);
if (isGzip)
{
if (chunkLength > 0)
contentStream.Write(bytes, i + 2, chunkLength);
}
else
{
html += coding.GetString(bytes, i + 2, chunkLength);
}
i = i + 4 + chunkLength;
if (i + 1 >= contentLength + contentStartIndex)
{
//此步骤很有必要,因之前的write操作已将流指向最后,如果要重新读取则必须重置
contentStream.Seek(0, SeekOrigin.Begin);
html = GetTextForGzip(contentStream, coding);
}
lastEnterIndex = i;
}
}
return html;
}
示例5: FormFile
public FormFile(byte[] infob, byte[] content, Encoding encoding)
{
var info = encoding.GetString(infob);
ContentType = null;
Size = -1;
var parts = info.Split(new[] {";", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts.Select(x => x.Trim()))
{
if (part.StartsWith("name=", StringComparison.OrdinalIgnoreCase))
{
Name = UnEscape(part.Substring(5));
}
else if (part.StartsWith("filename=", StringComparison.OrdinalIgnoreCase))
{
FileName = UnEscape(part.Substring(9));
Size = content.Length;
}
else if (part.StartsWith("Content-Type: ", StringComparison.OrdinalIgnoreCase))
{
ContentType = UnEscape(part.Substring(14));
}
}
_stream = ContentType != null ? new MemoryStream(content) : null;
if (ContentType == null)
FileName = encoding.GetString(content);
}
示例6: GetHtml
// 获取网页的HTML内容,根据网页的charset自动判断Encoding
public static string GetHtml(string url, Encoding encoding = null)
{
byte[] buf = new WebClient().DownloadData(url);
if (encoding != null) return encoding.GetString(buf);
string html = Encoding.UTF8.GetString(buf);
encoding = GetEncodingFromHtml(html);
if (encoding == null || encoding == Encoding.UTF8) return html;
return encoding.GetString(buf);
}
示例7: BasicAuth
public BasicAuth(string encodedHeader, Encoding encoding)
{
HeaderValue = encodedHeader;
var decodedHeader = encodedHeader.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase)
? encoding.GetString(Convert.FromBase64String(encodedHeader.Substring(Prefix.Length)))
: encoding.GetString(Convert.FromBase64String(encodedHeader));
var credArray = decodedHeader.Split(':');
if (credArray.Length > 0)
_User = credArray[0];
if (credArray.Length > 1)
_Password = credArray[1];
}
示例8: ReadMessage
private static Message ReadMessage(BinaryReader BR, Encoding E)
{
Message M = new Message();
M.Category = BR.ReadByte();
M.Language = BR.ReadByte();
M.ParentGroup = BR.ReadByte();
M.ID = BR.ReadByte();
M.Text = E.GetString(BR.ReadBytes(BR.ReadByte())).TrimEnd('\0');
if (M.Category == 4)
M.AlternateText = E.GetString(BR.ReadBytes(BR.ReadByte())).TrimEnd('\0');
return M;
}
示例9: ReadMessageGroup
private static MessageGroup ReadMessageGroup(BinaryReader BR, Encoding E)
{
MessageGroup MG = new MessageGroup();
MG.Category = BR.ReadByte();
MG.Language = BR.ReadByte();
MG.ID = BR.ReadByte();
MG.ParentGroup = BR.ReadByte();
MG.Title = E.GetString(BR.ReadBytes(32)).TrimEnd('\0');
MG.Description = E.GetString(BR.ReadBytes(32)).TrimEnd('\0');
uint MessageCount = BR.ReadUInt32();
/* MessageBytes */ BR.ReadUInt32();
for (int i = 0; i < MessageCount; ++i)
MG.Messages.Add(AutoTranslator.ReadMessage(BR, E));
return MG;
}
示例10: PostData
/// <summary>
/// 利用WebClient 远程POST数据并返回数据
/// </summary>
/// <param name="strUrl">远程URL地址</param>
/// <param name="strParams">参数</param>
/// <param name="RespEncode">POST数据的编码</param>
/// <param name="ReqEncode">获取数据的编码</param>
/// <returns></returns>
public static string PostData(string strUrl, string strParams, Encoding RespEncode, Encoding ReqEncode)
{
HttpClient httpclient = new HttpClient();
try
{
//打开页面
httpclient.Credentials = CredentialCache.DefaultCredentials;
//从指定的URI下载资源
byte[] responseData = httpclient.DownloadData(strUrl);
string srcString = RespEncode.GetString(responseData);
httpclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string postString = strParams;
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 上传数据,返回页面的字节数组
responseData = httpclient.UploadData(strUrl, "POST", postData);
srcString = ReqEncode.GetString(responseData);
return srcString;
}
catch (Exception ex)
{
//记录异常日志
//释放资源
httpclient.Dispose();
return string.Empty;
}
}
示例11: MailTemplet
/// <summary>
/// 新建一个邮件模板对象
/// </summary>
/// <param name="templetFilePath">HTML模块文件路径</param>
/// <param name="fileEnc">模板内容编码方式</param>
public MailTemplet(string templetFilePath, Encoding fileEnc)
{
if (!File.Exists(templetFilePath))
throw new ArgumentException("模块文件在指定路径不存在!");
tptContent = fileEnc.GetString(File.ReadAllBytes(templetFilePath));
}
示例12: ConvertBinaryToString
/// <inheritdoc/>
public string ConvertBinaryToString(Encoding encoding, byte[] buffer)
{
Requires.NotNull(encoding, "encoding");
Requires.NotNull(buffer, "buffer");
return encoding.GetString(buffer, 0, buffer.Length);
}
示例13: GetContents
public string GetContents(Encoding enc)
{
var buffer = new byte[mem.Length];
mem.Position = 0;
mem.Read(buffer, 0, buffer.Length);
return enc.GetString(buffer, 0, buffer.Length);
}
示例14: ConvertEncoding
public static string ConvertEncoding(string srcStr, Encoding srcEnc, Encoding destEnc)
{
byte[] bytes = srcEnc.GetBytes(srcStr);
bytes = Encoding.Convert(srcEnc, destEnc, bytes);
return destEnc.GetString(bytes);
}
示例15: GetString
/// <summary>
/// Gets the string from the byte array using the specified encoding.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="encoding">The encoding.</param>
/// <returns>System.String.</returns>
public static string GetString(this byte[] data, Encoding encoding)
{
Argument.IsNotNull("data", data);
Argument.IsNotNull("encoding", encoding);
return encoding.GetString(data, 0, data.Length);
}