本文整理汇总了C#中System.IO.MemoryStream.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.MemoryStream.ToString方法的具体用法?C# System.IO.MemoryStream.ToString怎么用?C# System.IO.MemoryStream.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了System.IO.MemoryStream.ToString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Translate
/*
http://stebet.net/httpclient-best-practices/
http://tiku.io/questions/173712/make-https-call-using-httpclient
http://www.dotnetcurry.com/showarticle.aspx?ID=1029
*/
public async Task<string> Translate ()
{
string url = string.Format
(
uri_google_translate
, Uri.EscapeUriString (text_to_translate)
, culture_to_translate
, culture_translated
);
// Create a Language mapping
var languageMap = new Dictionary<string, string> ();
InitLanguageMap (languageMap);
// Create an instance of WebClient in order to make the language translation
//Uri address = new Uri(uri_google_translate);
string address =
//@"http://holisticware.net/holisticware"
@"http://google.com"
;
using (var client = new HttpClient ())
{
try
{
var handler = new HttpClientHandler();
/*
Problem:
System.MissingMethodException: Method not found: 'System.Net.Http.HttpClientHandler.set_AutomaticDecompression'.
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[System.String].Start[<Translate>c__async0] (HolisticWare.Core.Localization.Translate.<Translate>c__async0& stateMachine) [0x0001b]
in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/AsyncTaskMethodBuilder_T.cs:107
at HolisticWare.Core.Localization.Translate.GoogleTranslatePrimitive.Translate () [0x00000]
in <filename unknown>:0
at HolisticWare.BabelFish.MainPage+<buttonTranslate_Clicked>c__async0.MoveNext () [0x0002d]
in /Users/moljac/Projects/HolisticWare/HolisticWare.BabelFish/HolisticWare.BabelFish/HolisticWare.BabelFish/MainPage.xaml.cs:23
Solution:
add the portable HTTP client to BOTH your portable class library AND any app that consumes that assembly
*/
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression =
System.Net.DecompressionMethods.GZip
|
System.Net.DecompressionMethods.Deflate
;
}
var httpClient = new HttpClient (handler);
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0");
client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");
string string_response = "";
/*
The code will not decompress
var response_get_wit_uri = await client.GetAsync(new Uri (address));
response_get_wit_uri.EnsureSuccessStatusCode ();
string_response = await response_get_wit_uri.Content.ReadAsStringAsync ();
*/
// http://blogs.msdn.com/b/dotnet/archive/2013/06/06/portable-compression-and-httpclient-working-together.aspx
var result = await client.GetStringAsync(url);
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(result);
//GZipStream bigStream = new GZipStream(new System.IO.MemoryStream(byteArray), CompressionMode.Decompress);
System.IO.MemoryStream bigStreamOut = new System.IO.MemoryStream();
//bigStream.CopyTo(bigStreamOut);
string s = bigStreamOut.ToString();
var response_get_wit_string = await client.GetAsync(address);
response_get_wit_string.EnsureSuccessStatusCode ();
string_response = await response_get_wit_string.Content.ReadAsStringAsync ();
}
catch (System.ArgumentException exc)
{
/*
Problem:
System.ArgumentException: Encoding name 'ISO-8859-2' not supported Parameter name: name
at System.Text.Encoding.GetEncoding (System.String name) [0x002f6]
//.........这里部分代码省略.........
示例2: SerializeObject
/// <summary>
/// ����������ֻ�õ�cookie�� ���л�
/// </summary>
/// <param name="obj">����</param>
/// <returns></returns>
public static string SerializeObject(object obj)
{
System.Runtime.Serialization.IFormatter bf = new BinaryFormatter();
string result = string.Empty;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
bf.Serialize(ms, obj);
byte[] byt = Encoding.UTF8.GetBytes(ms.ToString());//new byte[ms.length];Ҳ��
byt = ms.ToArray();
result = System.Convert.ToBase64String(byt);
ms.Flush();
}
return result;
}