By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

error when uploading image to fastpic.ru by C#

P: 1
Hi
I want to upload image to fastpic.ru
this is my code:
Expand|Select|Wrap|Line Numbers
  1. public string UploadImageToFastPic(string uploadfile, string url,string fileFormName, string contenttype)
  2.         {
  3.             if ((fileFormName == null) || (fileFormName.Length == 0))
  4.             {
  5.                 fileFormName = "file";
  6.             }
  7.  
  8.             if ((contenttype == null) || (contenttype.Length == 0))
  9.             {
  10.                 contenttype = "application/octet-stream";
  11.             }
  12.  
  13.             string boundary = "------WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
  14.  
  15.             HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
  16.             webrequest.CookieContainer = new CookieContainer();
  17.             webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
  18.             webrequest.Method = "POST";
  19.  
  20.  
  21.             // Build up the post message header
  22.             StringBuilder sb = new StringBuilder();
  23.             sb.Append(boundary);
  24.             sb.Append("\r\n");
  25.             sb.Append("Content-Dis-data; name=\"");
  26.             sb.Append(fileFormName);
  27.             sb.Append("\"; filename=\"");
  28.             sb.Append(Path.GetFileName(uploadfile));
  29.             sb.Append("\"");
  30.             sb.Append("\r\n");
  31.             sb.Append("Content-Type: ");
  32.             sb.Append(contenttype);
  33.             sb.Append("\r\n");
  34.             sb.Append("\r\n");
  35.  
  36.             sb.Append(boundary);
  37.             sb.Append("\r\n");
  38.             sb.Append("Content-Dis-data; name=\"check_thumb\"");
  39.             sb.Append("\r\n");
  40.             sb.Append("\r\n");
  41.  
  42.             sb.Append("size");
  43.             sb.Append("\r\n");
  44.             sb.Append(boundary);
  45.             sb.Append("\r\n");
  46.             sb.Append("Content-Dis-data; name=\"thumb_text\"");
  47.             sb.Append("\r\n");
  48.             sb.Append("\r\n");
  49.  
  50.             sb.Append("Увеличить");
  51.             sb.Append("\r\n");
  52.             sb.Append(boundary);
  53.             sb.Append("\r\n");
  54.             sb.Append("Content-Dis-data; name=\"thumb_size\"");
  55.             sb.Append("\r\n");
  56.             sb.Append("\r\n");
  57.  
  58.             sb.Append("170");
  59.             sb.Append("\r\n");
  60.             sb.Append(boundary);
  61.             sb.Append("\r\n");
  62.             sb.Append("Content-Dis-data; name=\"res_select\"");
  63.             sb.Append("\r\n");
  64.             sb.Append("\r\n");
  65.  
  66.             sb.Append("500");
  67.             sb.Append("\r\n");
  68.             sb.Append(boundary);
  69.             sb.Append("\r\n");
  70.             sb.Append("Content-Dis-data; name=\"orig_resize\"");
  71.             sb.Append("\r\n");
  72.             sb.Append("\r\n");
  73.  
  74.             sb.Append("500");
  75.             sb.Append("\r\n");
  76.             sb.Append(boundary);
  77.             sb.Append("\r\n");
  78.             sb.Append("Content-Dis-data; name=\"orig_rotate\"");
  79.             sb.Append("\r\n");
  80.             sb.Append("\r\n");
  81.  
  82.             sb.Append("0");
  83.             sb.Append("\r\n");
  84.             sb.Append(boundary);
  85.             sb.Append("\r\n");
  86.             sb.Append("Content-Dis-data; name=\"jpeg_quality\"");
  87.             sb.Append("\r\n");
  88.             sb.Append("\r\n");
  89.  
  90.             sb.Append("75");
  91.             sb.Append(boundary);
  92.             sb.Append("Content-Dis-data; name=\"submit\"");
  93.             sb.Append("\r\n");
  94.             sb.Append("\r\n");
  95.  
  96.             sb.Append("Загрузить");
  97.             sb.Append("\r\n");
  98.             sb.Append(boundary);
  99.             sb.Append("\r\n");
  100.             sb.Append("Content-Dis-data; name=\"uploading\"");
  101.             sb.Append("\r\n");
  102.             sb.Append("\r\n");
  103.  
  104.             sb.Append("1");
  105.             sb.Append("\r\n");
  106.             sb.Append(boundary);
  107.             sb.Append("--");
  108.  
  109.             string postHeader = sb.ToString();
  110.             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
  111.  
  112.             // Build the trailing boundary string as a byte array
  113.             // ensuring the boundary appears on a line by itself
  114.             byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
  115.  
  116.             FileStream fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
  117.             long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
  118.             webrequest.ContentLength = length;
  119.  
  120.             Stream requestStream = webrequest.GetRequestStream();
  121.  
  122.             // Write out our post header
  123.             requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
  124.  
  125.             // Write out the file contents
  126.             byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
  127.             int bytesRead = 0;
  128.             while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  129.                 requestStream.Write(buffer, 0, bytesRead);
  130.  
  131.             // Write out the trailing boundary
  132.             requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
  133.             WebResponse responce = webrequest.GetResponse();
  134.             Stream s = responce.GetResponseStream();
  135.             StreamReader sr = new StreamReader(s);
  136.  
  137.             return sr.ReadToEnd();
  138.  
this is my code at from main:
Expand|Select|Wrap|Line Numbers
  1. richTextBox_FullStory.Text = pro.UploadImageToFastPic("E:\\The Haves.jpg","fastpic.ru/uploadmulti", "file[]", "image/jpeg");
I want return result when done function UploadImageToFastpic is content of file .html uploaded file image succussfully

but when function UploadImageToFastpic finished so result return still content of file .html when not uploaded image to fastpic.ru yet

I debug and I realize can't get cookies

SOMEBODY HELP ME, PLEASE !!!!

P/s: the first enter click to fastpic.ru that it appear popup.I don't know thing influence to my code

thank you so much for your help !!!!!!!!!!!
Jun 3 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.