)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Http (Ia.Cl.Models)

Public general use code classes and xml files that we've compiled and used over the years:

Contains functions that relate to posting and receiving data from remote Internet/Intranet pages

    1: using System;
    2: using System.IO;
    3: using System.Net;
    4: using System.Text;
    5: using System.Threading.Tasks;
    6: using System.Net.Http;
    7: using System.Net.Http.Headers;
    8: using System.Net.Http.Json;
    9: using Newtonsoft.Json;
   10:  
   11: namespace Ia.Cl.Models
   12: {
   13:     ////////////////////////////////////////////////////////////////////////////
   14:  
   15:     /// <summary publish="true">
   16:     /// Contains functions that relate to posting and receiving data from remote Internet/Intranet pages
   17:     /// </summary>
   18:     /// <remarks> 
   19:     /// Copyright � 2001-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   20:     ///
   21:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   22:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   23:     ///
   24:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   25:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   26:     /// 
   27:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   28:     /// 
   29:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   30:     /// </remarks> 
   31:     public class Http
   32:     {
   33:         ////////////////////////////////////////////////////////////////////////////
   34:  
   35:         /// <summary>
   36:         ///
   37:         /// </summary>
   38:         private static bool range = false;
   39:  
   40:         ////////////////////////////////////////////////////////////////////////////
   41:  
   42:         /// <summary>
   43:         ///
   44:         /// </summary>
   45:         public Http() { }
   46:  
   47:         // Note that "https://" and "http://" are different. Wrong protocol could produce a "(403) Forbidden" response.
   48:  
   49:         // Include custom cookies, start and end points, and posting of data to remove server.
   50:  
   51:         // See https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
   52:  
   53:         // Remember to "synch all the way"
   54:  
   55:         ////////////////////////////////////////////////////////////////////////////
   56:  
   57:         /// <summary>
   58:         /// 
   59:         /// </summary>
   60:         public static async Task<string> PostAsync<T>(string baseAddress, string serviceUrl, T t)
   61:         {
   62:             HttpResponseMessage httpResponseMessage;
   63:             var s = string.Empty;
   64:  
   65:             serviceUrl = SuffixUrlWithSlashIfItContainsDot(serviceUrl);
   66:  
   67:             using (var httpClient = new HttpClient())
   68:             {
   69:                 httpClient.BaseAddress = new Uri(baseAddress);
   70:                 httpClient.DefaultRequestHeaders.Accept.Clear();
   71:                 httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
   72:  
   73:                 HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(t), Encoding.UTF8, "application/json");
   74:  
   75:                 try
   76:                 {
   77:                     httpResponseMessage = await httpClient.PostAsJsonAsync(serviceUrl, httpContent);
   78:                     // e.g. httpResponseMessage = await httpClient.PostAsJsonAsync("api/products", product);
   79:  
   80:                     httpResponseMessage.EnsureSuccessStatusCode();
   81:  
   82:                     s = await httpResponseMessage.Content.ReadAsStringAsync();
   83:                 }
   84:                 catch (Exception e)
   85:                 {
   86:                 }
   87:             }
   88:  
   89:             return s;
   90:         }
   91:  
   92:         ////////////////////////////////////////////////////////////////////////////
   93:  
   94:         /// <summary>
   95:         /// 
   96:         /// </summary>
   97:         public static async Task<string> PostAsync(string baseAddress, string serviceUrl)
   98:         {
   99:             HttpResponseMessage httpResponseMessage;
  100:             var s = string.Empty;
  101:  
  102:             serviceUrl = SuffixUrlWithSlashIfItContainsDot(serviceUrl);
  103:  
  104:             using (var httpClient = new HttpClient())
  105:             {
  106:                 httpClient.BaseAddress = new Uri(baseAddress);
  107:                 httpClient.DefaultRequestHeaders.Accept.Clear();
  108:                 httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  109:  
  110:                 try
  111:                 {
  112:                     HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(string.Empty), Encoding.UTF8, "application/json"); // dummy
  113:  
  114:                     httpResponseMessage = await httpClient.PostAsJsonAsync(serviceUrl, httpContent);
  115:                     // e.g. httpResponseMessage = await httpClient.PostAsJsonAsync("api/products", product);
  116:  
  117:                     httpResponseMessage.EnsureSuccessStatusCode();
  118:  
  119:                     s = await httpResponseMessage.Content.ReadAsStringAsync();
  120:                 }
  121:                 catch (Exception e)
  122:                 {
  123:                 }
  124:             }
  125:  
  126:             return s;
  127:         }
  128:  
  129:         ////////////////////////////////////////////////////////////////////////////
  130:  
  131:         /// <summary>
  132:         /// 
  133:         /// </summary>
  134:         public static async Task<T> GetAsync<T>(string baseAddress, string serviceUrl)
  135:         {
  136:             HttpResponseMessage httpResponseMessage;
  137:             T t;
  138:  
  139:             t = default;
  140:  
  141:             serviceUrl = SuffixUrlWithSlashIfItContainsDot(serviceUrl);
  142:  
  143:             using (var httpClient = new HttpClient())
  144:             {
  145:                 httpClient.BaseAddress = new Uri(baseAddress);
  146:                 httpClient.DefaultRequestHeaders.Accept.Clear();
  147:                 httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  148:  
  149:                 try
  150:                 {
  151:                     httpResponseMessage = await httpClient.GetAsync(serviceUrl);
  152:  
  153:                     if (httpResponseMessage.IsSuccessStatusCode)
  154:                     {
  155:                         t = await httpResponseMessage.Content.ReadFromJsonAsync<T>(); //.ReadAsAsync<T>();
  156:                                                                                       // see https://stackoverflow.com/questions/63108280/has-httpcontent-readasasynct-method-been-superceded-in-net-core
  157:                     }
  158:                     else
  159:                     {
  160:  
  161:                     }
  162:                 }
  163:                 catch (Exception e)
  164:                 {
  165:                 }
  166:             }
  167:  
  168:             return t;
  169:         }
  170:  
  171:         ////////////////////////////////////////////////////////////////////////////
  172:  
  173:         /// <summary>
  174:         /// 
  175:         /// </summary>
  176:         public static async Task<string> GetAsync(string baseAddress, string serviceUrl)
  177:         {
  178:             HttpResponseMessage httpResponseMessage;
  179:             var s = string.Empty;
  180:  
  181:             serviceUrl = SuffixUrlWithSlashIfItContainsDot(serviceUrl);
  182:  
  183:             using (var httpClient = new HttpClient())
  184:             {
  185:                 try
  186:                 {
  187:                     httpClient.BaseAddress = new Uri(baseAddress);
  188:                     httpClient.DefaultRequestHeaders.Accept.Clear();
  189:                     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  190:  
  191:                     httpResponseMessage = await httpClient.GetAsync(serviceUrl);
  192:  
  193:                     httpResponseMessage.EnsureSuccessStatusCode();
  194:  
  195:                     s = await httpResponseMessage.Content.ReadAsStringAsync();
  196:                 }
  197:                 catch (Exception e)
  198:                 {
  199:                 }
  200:             }
  201:  
  202:             return s;
  203:         }
  204:  
  205:         ////////////////////////////////////////////////////////////////////////////
  206:  
  207:         /// <summary>
  208:         /// 
  209:         /// </summary>
  210:         public static async Task<string> PutAsync<T>(string baseAddress, string serviceUrl, T t)
  211:         {
  212:             HttpResponseMessage httpResponseMessage;
  213:             var s = string.Empty;
  214:  
  215:             serviceUrl = SuffixUrlWithSlashIfItContainsDot(serviceUrl);
  216:  
  217:             using (var httpClient = new HttpClient())
  218:             {
  219:                 try
  220:                 {
  221:                     httpClient.BaseAddress = new Uri(baseAddress);
  222:                     httpClient.DefaultRequestHeaders.Accept.Clear();
  223:                     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  224:  
  225:                     HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(t), Encoding.UTF8, "application/json");
  226:  
  227:                     httpResponseMessage = await httpClient.PutAsJsonAsync(serviceUrl, httpContent);
  228:                     // e.g. httpResponseMessage = await httpClient.PutAsJsonAsync($"api/products/{product.Id}", product);
  229:  
  230:                     httpResponseMessage.EnsureSuccessStatusCode();
  231:  
  232:                     s = await httpResponseMessage.Content.ReadAsStringAsync();
  233:                 }
  234:                 catch (Exception e)
  235:                 {
  236:                 }
  237:             }
  238:  
  239:             return s;
  240:         }
  241:  
  242:         ////////////////////////////////////////////////////////////////////////////
  243:  
  244:         /// <summary>
  245:         /// 
  246:         /// </summary>
  247:         public static async Task<string> PutAsync(string baseAddress, string serviceUrl)
  248:         {
  249:             HttpResponseMessage httpResponseMessage;
  250:             var s = string.Empty;
  251:  
  252:             serviceUrl = SuffixUrlWithSlashIfItContainsDot(serviceUrl);
  253:  
  254:             using (var httpClient = new HttpClient())
  255:             {
  256:                 try
  257:                 {
  258:                     httpClient.BaseAddress = new Uri(baseAddress);
  259:                     httpClient.DefaultRequestHeaders.Accept.Clear();
  260:                     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  261:  
  262:                     HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(string.Empty), Encoding.UTF8, "application/json"); // dummy
  263:  
  264:                     httpResponseMessage = await httpClient.PutAsJsonAsync(serviceUrl, httpContent);
  265:                     // e.g. httpResponseMessage = await httpClient.PutAsJsonAsync($"api/products/{product.Id}", product);
  266:  
  267:                     httpResponseMessage.EnsureSuccessStatusCode();
  268:  
  269:                     s = await httpResponseMessage.Content.ReadAsStringAsync();
  270:                 }
  271:                 catch (Exception e)
  272:                 {
  273:                 }
  274:             }
  275:  
  276:             return s;
  277:         }
  278:  
  279:         ////////////////////////////////////////////////////////////////////////////
  280:  
  281:         /// <summary>
  282:         /// 
  283:         /// </summary>
  284:         public static async Task<string> DeleteAsync(string baseAddress, string serviceUrl)
  285:         {
  286:             HttpResponseMessage httpResponseMessage;
  287:             var s = string.Empty;
  288:  
  289:             serviceUrl = SuffixUrlWithSlashIfItContainsDot(serviceUrl);
  290:  
  291:             using (var httpClient = new HttpClient())
  292:             {
  293:                 try
  294:                 {
  295:                     httpClient.BaseAddress = new Uri(baseAddress);
  296:                     httpClient.DefaultRequestHeaders.Accept.Clear();
  297:                     httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  298:  
  299:                     httpResponseMessage = await httpClient.DeleteAsync(serviceUrl);
  300:                     // e.g. httpResponseMessage = await client.DeleteAsync($"api/products/{id}");
  301:  
  302:                     httpResponseMessage.EnsureSuccessStatusCode();
  303:  
  304:                     s = await httpResponseMessage.Content.ReadAsStringAsync();
  305:                 }
  306:                 catch (Exception e)
  307:                 {
  308:                 }
  309:             }
  310:  
  311:             return s;
  312:         }
  313:  
  314:         ////////////////////////////////////////////////////////////////////////////
  315:  
  316:         /// <summary>
  317:         ///
  318:         /// </summary>
  319:         private static string SuffixUrlWithSlashIfItContainsDot(string url)
  320:         {
  321:             // - Suffix the URL with a slash e.g. http://somedomain.com/api/people/staff.33311/ instead of http://somedomain.com/api/people/staff.33311 to pass a dot '.'
  322:  
  323:             if (!string.IsNullOrEmpty(url))
  324:             {
  325:                 if (url.Contains(".")) url += "/";
  326:             }
  327:  
  328:             return url;
  329:         }
  330:  
  331:         ////////////////////////////////////////////////////////////////////////////
  332:         ////////////////////////////////////////////////////////////////////////////
  333:  
  334:  
  335:  
  336:         ////////////////////////////////////////////////////////////////////////////
  337:         ////////////////////////////////////////////////////////////////////////////
  338:  
  339:         /// <summary>
  340:         ///
  341:         /// </summary>
  342:         public static string Request(string url)
  343:         {
  344:             range = false;
  345:  
  346:             return ProcessRequest(url, 0, false, null, null);
  347:         }
  348:  
  349:         ////////////////////////////////////////////////////////////////////////////
  350:  
  351:         /// <summary>
  352:         ///
  353:         /// </summary>
  354:         public static string Request(string url, int start)
  355:         {
  356:             range = true;
  357:  
  358:             return ProcessRequest(url, start, false, null, null);
  359:         }
  360:  
  361:         ////////////////////////////////////////////////////////////////////////////
  362:  
  363:         /// <summary>
  364:         ///
  365:         /// </summary>
  366:         public static string Request2(string url)
  367:         {
  368:             range = true;
  369:  
  370:             return ProcessRequest2(url, false);
  371:         }
  372:  
  373:         ////////////////////////////////////////////////////////////////////////////
  374:  
  375:         /// <summary>
  376:         ///
  377:         /// </summary>
  378:         public static string Request_Utf8(string url, int start)
  379:         {
  380:             range = true;
  381:  
  382:             return ProcessRequest(url, start, true, null, null);
  383:         }
  384:  
  385:         ////////////////////////////////////////////////////////////////////////////
  386:  
  387:         /// <summary>
  388:         ///
  389:         /// </summary>
  390:         public static string Request(string url, int start, System.Net.Cookie c)
  391:         {
  392:             range = true;
  393:  
  394:             return ProcessRequest(url, start, false, c, null);
  395:         }
  396:  
  397:         ////////////////////////////////////////////////////////////////////////////
  398:  
  399:         /// <summary>
  400:         ///
  401:         /// </summary>
  402:         public static string Request(string url, int start, System.Net.Cookie c1, System.Net.Cookie c2)
  403:         {
  404:             range = true;
  405:  
  406:             return ProcessRequest(url, start, false, c1, c2);
  407:         }
  408:  
  409:         ////////////////////////////////////////////////////////////////////////////
  410:  
  411:         /// <summary>
  412:         ///
  413:         /// </summary>
  414:         public static string Post(string URI, string Parameters)
  415:         {
  416:             System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
  417:             //req.Proxy = new System.Net.WebProxy(ProxyString, true);
  418:  
  419:             req.ContentType = "application/x-www-form-urlencoded";
  420:             req.Method = "POST";
  421:             //req.Timeout = 3000;
  422:  
  423:             byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
  424:             req.ContentLength = bytes.Length;
  425:  
  426:             using (System.IO.Stream os = req.GetRequestStream())
  427:             {
  428:                 os.Write(bytes, 0, bytes.Length);
  429:                 //os.Close();
  430:             }
  431:  
  432:             System.Net.WebResponse resp = null;
  433:  
  434:             try
  435:             {
  436:                 resp = req.GetResponse();
  437:  
  438:                 if (resp == null) return null;
  439:  
  440:                 System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream(), Encoding.GetEncoding(1256));
  441:                 return sr.ReadToEnd().Trim();
  442:             }
  443:             catch (WebException ex)
  444:             {
  445:                 string str = ex.Message;
  446:             }
  447:  
  448:             return null;
  449:         }
  450:  
  451:         ////////////////////////////////////////////////////////////////////////////
  452:  
  453:         /// <summary>
  454:         ///
  455:         /// </summary>
  456:         public static string Post(string URI, string Parameters, int code_page)
  457:         {
  458:             // Sometimes you need to POST in Windows 1256 code page for the process to run
  459:  
  460:             System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
  461:             //req.Proxy = new System.Net.WebProxy(ProxyString, true);
  462:  
  463:             req.ContentType = "application/x-www-form-urlencoded";
  464:             req.Method = "POST";
  465:             //req.Timeout = 3000;
  466:  
  467:             byte[] bytes = System.Text.Encoding.GetEncoding(code_page).GetBytes(Parameters);
  468:             req.ContentLength = bytes.Length;
  469:  
  470:             using (System.IO.Stream os = req.GetRequestStream())
  471:             {
  472:                 os.Write(bytes, 0, bytes.Length);
  473:                 //os.Close();
  474:             }
  475:  
  476:             System.Net.WebResponse resp = null;
  477:  
  478:             try
  479:             {
  480:                 resp = req.GetResponse();
  481:  
  482:                 if (resp == null) return null;
  483:  
  484:                 System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream(), Encoding.GetEncoding(code_page));
  485:                 return sr.ReadToEnd().Trim();
  486:             }
  487:             catch (WebException ex)
  488:             {
  489:                 string str = ex.Message;
  490:             }
  491:  
  492:             return null;
  493:         }
  494:  
  495:         ////////////////////////////////////////////////////////////////////////////
  496:  
  497:         /// <summary>
  498:         ///
  499:         /// </summary>
  500:         private static string ProcessRequest(string url, int start, bool utf8, System.Net.Cookie c1, System.Net.Cookie c2)
  501:         {
  502:             string text = "";
  503:  
  504:             try
  505:             {
  506:                 Uri ourUri = new Uri(url);
  507:                 // Creates an HttpWebRequest for the specified URL. 
  508:                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri);
  509:  
  510:                 // this code below is very important. It sends a request with a specific cookie in the collection
  511:                 // to demonstrate to the remote server that we have his cookie and we should skip his advertisement.
  512:                 if (c1 != null || c2 != null)
  513:                 {
  514:                     myHttpWebRequest.CookieContainer = new CookieContainer();
  515:                     if (c1 != null) myHttpWebRequest.CookieContainer.Add(c1);
  516:                     if (c2 != null) myHttpWebRequest.CookieContainer.Add(c2);
  517:                 }
  518:  
  519:                 myHttpWebRequest.Method = "POST";
  520:                 //myHttpWebRequest.Timeout = 5000; // 5 sec
  521:                 //myHttpWebRequest.MaximumResponseHeadersLength = 100; // *1024 (Kilobytes)
  522:  
  523:                 // set the range of data to be returned if the start and end positions are given
  524:                 if (range) myHttpWebRequest.AddRange(start);
  525:  
  526:                 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
  527:                 myHttpWebRequest.ContentLength = 0;
  528:  
  529:                 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
  530:  
  531:                 if (myHttpWebRequest.HaveResponse)
  532:                 {
  533:                     Stream receiveStream = myHttpWebResponse.GetResponseStream();
  534:                     Encoding encode;
  535:  
  536:                     if (utf8) encode = System.Text.Encoding.GetEncoding("utf-8");
  537:                     else encode = System.Text.Encoding.GetEncoding(1252); // 1252 best for western char
  538:  
  539:                     // Pipes the stream to a higher level stream reader with the required encoding format. 
  540:                     using (StreamReader readStream = new StreamReader(receiveStream, encode))
  541:                     {
  542:                         text = readStream.ReadToEnd().Trim();  // ONE
  543:  
  544:                         /*
  545:                         // TWO
  546:                         Char[] read = new Char[256];
  547:                         // Reads 256 characters at a time.    
  548:                         int count = readStream.Read( read, 0, 256 );
  549: 
  550:                         while (count > 0) 
  551:                         {
  552:                           // Dumps the 256 characters on a string and displays the string to the console.
  553:                           String str = new String(read, 0, count);
  554:                           text += str;
  555:                           count = readStream.Read(read, 0, 256);
  556:                         }
  557:                         */
  558:  
  559:                         // Releases the resources of the response.
  560:                         //myHttpWebResponse.Close();
  561:                     }
  562:                 }
  563:                 else
  564:                 {
  565:                     text = "\nResponse not received from server";
  566:                 }
  567:             }
  568:             catch (WebException e)
  569:             {
  570:                 HttpWebResponse response = (HttpWebResponse)e.Response;
  571:                 if (response != null)
  572:                 {
  573:                     if (response.StatusCode == HttpStatusCode.Unauthorized)
  574:                     {
  575:                         string challenge = null;
  576:                         challenge = response.GetResponseHeader("WWW-Authenticate");
  577:                         if (challenge != null) text = "\nThe following challenge was raised by the server: " + challenge;
  578:                     }
  579:                     else text = "\nThe following WebException was raised : " + e.Message;
  580:                 }
  581:                 else text = "\nResponse Received from server was null";
  582:             }
  583:             catch (Exception e)
  584:             {
  585:                 text = "\nThe following Exception was raised : " + e.Message;
  586:             }
  587:  
  588:             return text;
  589:         }
  590:  
  591:         ////////////////////////////////////////////////////////////////////////////
  592:  
  593:         /// <summary>
  594:         ///
  595:         /// </summary>
  596:         private static string ProcessRequest2(string url, bool utf8)
  597:         {
  598:             string text = "";
  599:  
  600:             try
  601:             {
  602:                 Uri ourUri = new Uri(url);
  603:                 // Creates an HttpWebRequest for the specified URL. 
  604:                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri);
  605:  
  606:                 //myHttpWebRequest.Method = "POST";
  607:                 //myHttpWebRequest.Timeout = 5000; // 5 sec
  608:                 //myHttpWebRequest.MaximumResponseHeadersLength = 100; // *1024 (Kilobytes)
  609:  
  610:                 // set the range of data to be returned if the start and end positions are given
  611:                 //if (range) myHttpWebRequest.AddRange(start);
  612:  
  613:                 myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
  614:                 myHttpWebRequest.ContentLength = 0;
  615:  
  616:                 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
  617:  
  618:                 if (myHttpWebRequest.HaveResponse)
  619:                 {
  620:                     Stream receiveStream = myHttpWebResponse.GetResponseStream();
  621:                     Encoding encode;
  622:  
  623:                     if (utf8) encode = System.Text.Encoding.GetEncoding("utf-8");
  624:                     else encode = System.Text.Encoding.GetEncoding(1252); // 1252 best for western char
  625:  
  626:                     // Pipes the stream to a higher level stream reader with the required encoding format. 
  627:                     using (StreamReader readStream = new StreamReader(receiveStream, encode))
  628:                     {
  629:                         text = readStream.ReadToEnd().Trim();  // ONE
  630:  
  631:                         /*
  632:                         // TWO
  633:                         Char[] read = new Char[256];
  634:                         // Reads 256 characters at a time.    
  635:                         int count = readStream.Read( read, 0, 256 );
  636: 
  637:                         while (count > 0) 
  638:                         {
  639:                           // Dumps the 256 characters on a string and displays the string to the console.
  640:                           String str = new String(read, 0, count);
  641:                           text += str;
  642:                           count = readStream.Read(read, 0, 256);
  643:                         }
  644:                         */
  645:  
  646:                         // Releases the resources of the response.
  647:                         //myHttpWebResponse.Close();
  648:                     }
  649:                 }
  650:                 else
  651:                 {
  652:                     text = "\nResponse not received from server";
  653:                 }
  654:             }
  655:             catch (WebException e)
  656:             {
  657:                 HttpWebResponse response = (HttpWebResponse)e.Response;
  658:                 if (response != null)
  659:                 {
  660:                     if (response.StatusCode == HttpStatusCode.Unauthorized)
  661:                     {
  662:                         string challenge = null;
  663:                         challenge = response.GetResponseHeader("WWW-Authenticate");
  664:                         if (challenge != null) text = "\nThe following challenge was raised by the server: " + challenge;
  665:                     }
  666:                     else text = "\nThe following WebException was raised : " + e.Message;
  667:                 }
  668:                 else text = "\nResponse Received from server was null";
  669:             }
  670:             catch (Exception e)
  671:             {
  672:                 text = "\nThe following Exception was raised : " + e.Message;
  673:             }
  674:  
  675:             return text;
  676:         }
  677:  
  678:         ////////////////////////////////////////////////////////////////////////////
  679:  
  680:         /// <summary>
  681:         ///
  682:         /// </summary>
  683:         private static int Get(string url, out string text, out string result)
  684:         {
  685:             int op;
  686:  
  687:             op = 0;
  688:             result = "";
  689:             text = "";
  690:  
  691:             try
  692:             {
  693:                 Uri ourUri = new Uri(url);
  694:                 // Creates an HttpWebRequest for the specified URL. 
  695:                 HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(ourUri);
  696:                 HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
  697:  
  698:                 Stream receiveStream = myHttpWebResponse.GetResponseStream();
  699:                 Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
  700:  
  701:                 // Pipes the stream to a higher level stream reader with the required encoding format. 
  702:                 using (StreamReader readStream = new StreamReader(receiveStream, encode))
  703:                 {
  704:                     Char[] read = new Char[256];
  705:                     // Reads 256 characters at a time.    
  706:                     int count = readStream.Read(read, 0, 256);
  707:  
  708:                     while (count > 0)
  709:                     {
  710:                         // Dumps the 256 characters on a string and displays the string to the console.
  711:                         String str = new String(read, 0, count);
  712:                         text += str;
  713:                         count = readStream.Read(read, 0, 256);
  714:                     }
  715:  
  716:                     // Releases the resources of the response.
  717:                     //myHttpWebResponse.Close();
  718:                 }
  719:  
  720:                 op = 1;
  721:             }
  722:             catch (WebException e)
  723:             {
  724:                 HttpWebResponse response = (HttpWebResponse)e.Response;
  725:                 if (response != null)
  726:                 {
  727:                     if (response.StatusCode == HttpStatusCode.Unauthorized)
  728:                     {
  729:                         string challenge = null;
  730:                         challenge = response.GetResponseHeader("WWW-Authenticate");
  731:                         if (challenge != null) result = "The following challenge was raised by the server: " + challenge;
  732:                     }
  733:                     else result = "The following WebException was raised : " + e.Message;
  734:                 }
  735:                 else result = "Response Received from server was null";
  736:  
  737:                 op = -1;
  738:             }
  739:             catch (Exception e)
  740:             {
  741:                 result = "The following Exception was raised : " + e.Message;
  742:                 op = -1;
  743:             }
  744:  
  745:             return op;
  746:         }
  747:  
  748:         ////////////////////////////////////////////////////////////////////////////
  749:         ////////////////////////////////////////////////////////////////////////////
  750:     }
  751: }