/*
			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] 
							in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Text/Encoding.cs:661    
							at System.Net.Http.HttpContent+<ReadAsStringAsync>c__async4.MoveNext () [0x0010e] 
							in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.0.51/src/mono/mcs/class/System.Net.Http/System.Net.Http/HttpContent.cs:176  
							--- End of stack trace from previous location where exception was thrown ---   
							at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] 
							in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62
							at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.String].GetResult () 
							[0x00034] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/ConfiguredTaskAwaitable_T.cs:62    
							at System.Net.Http.HttpClient+<GetStringAsync>c__async5.MoveNext () [0x000a9] 
							in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.0.51/src/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:322  
							--- End of stack trace from previous location where exception was thrown ---   
							at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] 
							in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62
							at System.Runtime.CompilerServices.TaskAwaiter`1[System.String].GetResult () [0x00034] 
							in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/TaskAwaiter_T.cs:59    
							at HolisticWare.Core.Localization.Translate.GoogleTranslatePrimitive+<Translate>c__async0.MoveNext () [0x0010e] 
							in /Users/moljac/Projects/HolisticWare/HolisticWare.BabelFish/HolisticWare.Core.Localization.Translate/GoogleTranslatePrimitive.cs:116 }	
							System.ArgumentException

							Related:

							http://blogs.msdn.com/b/dotnet/archive/2013/11/08/10435031.aspx?PageIndex=3

							Solution:

							client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0");
					*/
				}
				catch (System.Net.Http.HttpRequestException exc)
				{
					/*
						System.Net.Http.HttpRequestException: 403 (Forbidden: Access is denied.)   
							at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () [0x0000d] 
							in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.0.51/src/mono/mcs/class/System.Net.Http/System.Net.Http/HttpResponseMessage.cs:122    
							at HolisticWare.Core.Localization.Translate.GoogleTranslatePrimitive+<Translate>c__async0.MoveNext () [0x0013a] 
							in /Users/moljac/Projects/HolisticWare/HolisticWare.BabelFish/HolisticWare.Core.Localization.Translate/GoogleTranslatePrimitive.cs:125 }	
							System.Net.Http.HttpRequestException
					*/
					string msg = "";
				}
				catch (System.Exception exc)
				{

					string msg = "";
					System.Diagnostics.Debug.WriteLine(msg);
				}
			}
 
			/*
					Proxy for debugging 
						Fiddler, Charles

					for visual studio
					C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe.config


					 <system.net>
					    <defaultProxy useDefaultCredentials="true" enabled="true">
					      <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
					    </defaultProxy>
					  </system.net>

					foa all apps 

					Open machine.config in the folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config. Note that if you are debugging a 64bit service (like ASP.NET) you will want to look in the Framework64 folder instead of the Framework folder. Similarly, if you are using a .NET version prior to 4.0, you will need to adjust the version part of the path.

Add the following XML block as a peer to the existing system.net element, replacing any existing defaultProxy element if present:

<!-- The following section is to force use of Fiddler for all applications, including those running in service accounts -->
 <system.net>
 <defaultProxy
                 enabled = "true"
                 useDefaultCredentials = "true">
 <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
 </defaultProxy>
 </system.net>
  

				*/


			return text_translated;
		}
示例#2
0
        /// <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;
        }