private void PrimeConnection(string uri) { if (redirectHost == null) { // www.daxformatter.com redirects request to another site. HttpWebRequest does redirect with GET. It fails, since the web service works only with POST // The following 2 requests are doing manual POST re-direct //var webRequestFactory = IoC.Get<WebRequestFactory>(); var redirectRequest = (HttpWebRequest)WebRequest.Create(uri); redirectRequest.Proxy = ProxyCache.GetProxy(uri); redirectRequest.AllowAutoRedirect = false; redirectRequest.Timeout = Preferences.Current.DaxFormatterRequestTimeout; try { using (var netResponse = redirectRequest.GetResponse()) { var redirectResponse = (HttpWebResponse)netResponse; redirectUrl = redirectResponse.Headers["Location"]; var redirectUri = new Uri(redirectUrl); // set the shared redirectHost variable redirectHost = redirectUri.Host; } } catch { } } }
private string CallDaxFormatterMulti(string uri, List <string> dax, bool useSemicolonsAsSeparators, bool shortFormat, bool skipSpaceAfterFunctionName) { try { PrimeConnection(uri); var originalUri = new Uri(uri); var actualUri = new UriBuilder(originalUri.Scheme, redirectHost, originalUri.Port, originalUri.PathAndQuery).ToString(); var req = new DaxFormatterRequestMulti(useSemicolonsAsSeparators, shortFormat, skipSpaceAfterFunctionName) { Dax = dax }; var data = JsonConvert.SerializeObject(req, Formatting.Indented); var enc = System.Text.Encoding.UTF8; var data1 = enc.GetBytes(data); // this should allow DaxFormatter to work through http 1.0 proxies // see: http://stackoverflow.com/questions/566437/http-post-returns-the-error-417-expectation-failed-c //System.Net.ServicePointManager.Expect100Continue = false; var wr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(actualUri); wr.Proxy = ProxyCache.GetProxy(actualUri); wr.Timeout = Preferences.Current.DaxFormatterRequestTimeout; wr.Method = "POST"; wr.Accept = "application/json, text/javascript, */*; q=0.01"; wr.Headers.Add("Accept-Encoding", "gzip,deflate"); wr.Headers.Add("Accept-Language", "en-US,en;q=0.8"); wr.ContentType = "application/json; charset=UTF-8"; wr.AutomaticDecompression = DecompressionMethods.GZip; string output = ""; using (var strm = wr.GetRequestStream()) { strm.Write(data1, 0, data1.Length); using (var resp = wr.GetResponse()) { //var outStrm = new System.IO.Compression.GZipStream(resp.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress); var outStrm = resp.GetResponseStream(); using (var reader = new System.IO.StreamReader(outStrm)) { output = reader.ReadToEnd().Trim(); } } } return(output); } catch { throw; } finally { } }
public static string GetUrl(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Proxy = ProxyCache.GetProxy(url); request.Timeout = 10000; request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { return(reader.ReadToEnd()); } }
static void Main(string[] args) { ProxyCache cache = new ProxyCache(); URL url = new URL("electron://127.0.0.1:13187/"); FileNode node = cache.GetProxy(url); string path = node.CreateDirectory("electron", "hailg"); path = node.CreateDirectory("electron", path, "tieuchau"); string filePath = node.CreateFile("muon chut.xxx", path, true); node.WriteChunk(filePath, new byte[] { 1, 2, 3, 4, 5, 6 }); byte[] arr1 = node.ReadChunk(filePath, 0, 3); Console.WriteLine(arr1.Length); arr1 = node.ReadAll(filePath); Console.WriteLine(arr1.Length); Console.ReadLine(); }