Advertisement
Guest User

usermodeling

a guest
Dec 30th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Net.Http;
  3. using System.Web;
  4. using System.Net;
  5. using System.IO;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace WatsonWithWindowsForms
  10. {
  11.     class WatsonQA
  12.     {
  13.         private string url;
  14.         private string uid;
  15.         private string pwd;
  16.  
  17.         public WatsonQA(string baseURL, string uid, string pwd)
  18.         {
  19.             this.url = baseURL + "api/v2/profile/";
  20.             this.uid = uid;
  21.             this.pwd = pwd;
  22.  
  23.             Console.WriteLine("url:" + this.url);
  24.             Console.WriteLine("uid:" + this.uid);
  25.             Console.WriteLine("pwd:" + this.pwd);
  26.         }
  27.  
  28.         public string AskQuestion()
  29.         {
  30.             string answers = null;
  31.  
  32.             String line = "";
  33.             using (StreamReader sr = new StreamReader("C:\\Appl_VB\\TestWatson\\Data\\Comments\\test.txt"))
  34.             {
  35.                 line = sr.ReadToEnd();
  36.                 //Regex rgx = new Regex("[^a-zA-Z0-9 , .()!?$%]");
  37.                 //line = rgx.Replace(line, "");
  38.             }
  39.  
  40.             string data = "";
  41.             data += "{";
  42.             data+= "\"contentItems\": [";
  43.             data+= "{";
  44.             data+= "\"id\": \"MyUser\",";
  45.             data+= "\"userid\": \"author\",";
  46.             data+= "\"sourceid\": \"reddit\",";
  47.             data+= "\"contenttype\": \"text/plain\",";
  48.             data+= "\"language\": \"en\",";
  49.             data+= "\"content\":";
  50.             data+= "\"" + line + "\",";
  51.             data+= "}";
  52.             data+= "]";
  53.             data+= "}";
  54.  
  55.             string json = "{\"contentItems\": [{\"id\": \"xxx\",\"userid\": \"abc\", \"sourceid\": \"freetext\", \"contenttype\": \"text/plain\", \"language\": \"en\", \"content\": ";
  56.             json += "\"";
  57.             json += line;
  58.             json += "\"";
  59.             json += "}]}";
  60.  
  61.             var qaCall = (HttpWebRequest)WebRequest.Create(url);
  62.  
  63.             try
  64.             {
  65.                 string auth = string.Format("{0}:{1}", this.uid, this.pwd);
  66.                 string auth64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
  67.                 string credentials = string.Format("{0} {1}", "Basic", auth64);
  68.  
  69.                 qaCall.Headers[HttpRequestHeader.Authorization] = credentials;
  70.                 qaCall.Method = "POST";
  71.                 qaCall.Accept = "application/json";
  72.                 qaCall.ContentType = "application/json";
  73.  
  74.                 var encoding = new UTF8Encoding();
  75.                 var payload = Encoding.GetEncoding("iso-8859-1").GetBytes(json);
  76.                 qaCall.ContentLength = payload.Length;
  77.                 using (var callStream = qaCall.GetRequestStream())
  78.                 {
  79.                     callStream.Write(payload, 0, payload.Length);
  80.                 }
  81.             }
  82.             catch (Exception e)
  83.             {
  84.                 Console.Out.WriteLine("error:" + e.Message);
  85.                 Console.ReadKey();
  86.             }
  87.  
  88.             try
  89.             {
  90.                 WebResponse qaResponse = qaCall.GetResponse();
  91.                 Stream requestStream = qaResponse.GetResponseStream();
  92.                 StreamReader responseReader = new StreamReader(requestStream);
  93.                 answers = responseReader.ReadToEnd();
  94.                 responseReader.Close();
  95.             }
  96.             catch (System.Net.WebException e)
  97.             {
  98.                 string toto = e.Response.ToString();
  99.                 Console.Out.WriteLine("errors:" + e.Message);
  100.                 Console.ReadKey();
  101.             }
  102.             catch (Exception e)
  103.             {
  104.                 Console.Out.WriteLine("error:" + e.Message);
  105.                 Console.ReadKey();
  106.             }
  107.  
  108.             return answers;
  109.         }
  110.     }
  111.  
  112.  
  113.        
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement