Guest User

Untitled

a guest
Apr 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. public static string Create()
  2. {
  3. string authInfo = "username" + ":" + "password";
  4.  
  5. string root = AppDomain.CurrentDomain.BaseDirectory;
  6. string file = root + "Folder\work.txt";
  7.  
  8. FileInfo fi = new FileInfo(file);
  9. int fileLength = (int)fi.Length;
  10.  
  11. FileStream rdr = new FileStream(file, FileMode.Open);
  12.  
  13. HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  14. httpWebRequest.Method = "POST";
  15. httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  16. httpWebRequest.Accept = "application/xml";
  17.  
  18. authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
  19. httpWebRequest.Headers["Authorization"] = "Basic " + authInfo;
  20. byte[] requestBytes = new byte[fileLength];
  21.  
  22. int bytesRead = 0;
  23. httpWebRequest.ContentLength = requestBytes.Length;
  24. using (Stream requestStream = httpWebRequest.GetRequestStream())
  25. {
  26. while ((bytesRead = rdr.Read(requestBytes, 0, requestBytes.Length)) != 0)
  27. {
  28. requestStream.Write(requestBytes, 0, bytesRead);
  29. requestStream.Close();
  30. }
  31. }
  32. //READ RESPONSE FROM STREAM
  33. string responseData;
  34. using (StreamReader responseStream = new StreamReader(httpWebRequest.GetResponse().GetResponseStream()))
  35. {
  36. responseData = responseStream.ReadToEnd();
  37. responseStream.Close();
  38. }
  39. return responseData;
  40. }
Add Comment
Please, Sign In to add comment