Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Web;
  18.  
  19. namespace TwitterRequest
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class Twitter : Window
  25. {
  26.  
  27. string twitterUserName = "oetsieloetsie";
  28. string twitterPassword = "toettoetboingboing";
  29.  
  30. public Twitter()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. private void requestAnswerClick(object sender, RoutedEventArgs e)
  36. {
  37. HttpWebRequest request;
  38. HttpWebResponse response = null;
  39. StreamReader reader;
  40. StringBuilder sbSource;
  41. Uri address = new Uri("http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction");
  42.  
  43. if (address == null) { throw new ArgumentNullException("address"); }
  44.  
  45. try
  46. {
  47. // Create and initialize the web request
  48. request = WebRequest.Create(address) as HttpWebRequest;
  49. request.UserAgent = ".NET Sample";
  50. request.KeepAlive = false;
  51. // Set timeout to 15 seconds
  52. request.Timeout = 15 * 1000;
  53.  
  54. // Get response
  55. response = request.GetResponse() as HttpWebResponse;
  56.  
  57. if (request.HaveResponse == true && response != null)
  58. {
  59. // Get the response stream
  60. reader = new StreamReader(response.GetResponseStream());
  61.  
  62. // Read it into a StringBuilder
  63. sbSource = new StringBuilder(reader.ReadToEnd());
  64.  
  65. // Console application output
  66. respondAnswer.Text = sbSource.ToString();
  67. }
  68. }
  69. catch (WebException wex)
  70. {
  71. // This exception will be raised if the server didn't return 200 - OK
  72. // Try to retrieve more information about the network error
  73. if (wex.Response != null)
  74. {
  75. using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
  76. {
  77. Console.WriteLine(
  78. "The server returned '{0}' with the status code {1} ({2:d}).",
  79. errorResponse.StatusDescription, errorResponse.StatusCode,
  80. errorResponse.StatusCode);
  81. }
  82. }
  83. }
  84. finally
  85. {
  86. if (response != null) { response.Close(); }
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement