hlsdk

Steam avatar changer

Jun 16th, 2012
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. void Main()
  2. {
  3.     string URL = "http://steamcommunity.com/actions/FileUploader";
  4.     string boundary = "-----" + DateTime.Now.Ticks.ToString("x");
  5.     HttpWebRequest webRequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
  6.    
  7.     webRequest.Method = "POST";
  8.     webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
  9.     webRequest.CookieContainer = new CookieContainer();
  10.     webRequest.CookieContainer.Add(new Cookie("steamLogin", "<Your auth cookie goes here>", "/", "steamcommunity.com"));
  11.    
  12.     string FilePath = @"C:\Users\HLSDK\Desktop\draw\wat_novi.png";
  13.    
  14.     NameValueCollection formData = new NameValueCollection();
  15.  
  16.     formData.Clear();
  17.     formData["MAX_FILE_SIZE"] = "204800";
  18.     formData["type"] = "player_avatar_image";
  19.     formData["sId"] = "<64 bit steamid>";
  20.     formData["doSub"] = "0";
  21.    
  22.     Stream postDataStream = GetPostStream(FilePath, formData, boundary);
  23.    
  24.     webRequest.ContentLength = postDataStream.Length;
  25.     Stream reqStream = webRequest.GetRequestStream();
  26.    
  27.     postDataStream.Position = 0;
  28.    
  29.     byte[] buffer = new byte[1024];
  30.     int bytesRead = 0;
  31.    
  32.     while ((bytesRead = postDataStream.Read(buffer, 0, buffer.Length)) != 0)
  33.     {
  34.         reqStream.Write(buffer, 0, bytesRead);
  35.     }
  36.    
  37.     postDataStream.Close();
  38.     reqStream.Close();
  39.  
  40.     StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream());
  41.     string Result = sr.ReadToEnd();
  42.     Result.Dump();
  43. }
  44.  
  45. private static Stream GetPostStream(string filePath, NameValueCollection formData, string boundary)
  46. {
  47.     Stream postDataStream = new System.IO.MemoryStream();
  48.    
  49.     //adding form data
  50.     string formDataHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
  51.         "Content-Disposition: form-data; name=\"{0}\";" + Environment.NewLine + Environment .NewLine + "{1}";
  52.    
  53.     //adding file data
  54.     FileInfo fileInfo = new FileInfo(filePath);
  55.    
  56.     string fileHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
  57.         "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" +
  58.         Environment.NewLine + "Content-Type: image/png" + Environment.NewLine + Environment.NewLine;
  59.        
  60.     byte[] fileHeaderBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(fileHeaderTemplate,
  61.         "avatar", fileInfo.Name));
  62.    
  63.     postDataStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
  64.    
  65.     FileStream fileStream = fileInfo.OpenRead();
  66.    
  67.     byte[] buffer = new byte[1024];
  68.    
  69.     int bytesRead = 0;
  70.    
  71.     while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  72.     {
  73.         postDataStream.Write(buffer, 0, bytesRead);
  74.     }
  75.    
  76.     foreach (string key in formData.Keys)
  77.     {
  78.         byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(formDataHeaderTemplate,
  79.         key, formData[key]));
  80.         postDataStream.Write(formItemBytes, 0, formItemBytes.Length);
  81.     }
  82.    
  83.     fileStream.Close();
  84.    
  85.     byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--");
  86.     postDataStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  87.    
  88.     return postDataStream;
  89. }
  90.  
  91. // Define other methods and classes here
Advertisement
Add Comment
Please, Sign In to add comment