HttpWebResponse/HttpWebRequest upload image
C#
1
Posts
1
Posters
0
Views
1
Watching
-
i have prolem with uloadinge image to server i try to upload image and send data (POST) method a the same time i use this code
HttpWebResponse response; Stream resStream; StreamReader objSR; CookieContainer cookieContainer = new CookieContainer(); ASCIIEncoding encoding = new ASCIIEncoding(); HttpWebRequest request; string sResponse; string postData; byte\[\] data; string UrlLogin; //Upload Image Page \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* UrlLogin = "http://localhost:63637/whois/test0.aspx"; string uploadfile = "C:\\\\fax\\\\pics\\\\7130570.jpg"; string fileFormName = "userfile"; string contenttype = "image/pjpeg"; postData = "?wm\_flag=" + "0"; postData += ("&action=" + "process"); postData += ("&hi=" + "ft"); request = (HttpWebRequest)WebRequest.Create(UrlLogin + postData); //request.Accept = "\*/\*"; request.KeepAlive = true; request.CookieContainer = cookieContainer; string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); request.ContentType = "multipart/form-data; boundary=" + boundary; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; request.Method = "POST"; request.AllowAutoRedirect = true; data = encoding.GetBytes(postData); StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(boundary); sb.Append("\\r\\n"); sb.Append("Content-Disposition: form-data; name=\\""); sb.Append(fileFormName); sb.Append("\\"; filename=\\""); sb.Append(Path.GetFileName(uploadfile)); sb.Append("\\""); sb.Append("\\r\\n"); sb.Append("Content-Type: "); sb.Append(contenttype); sb.Append("\\r\\n"); sb.Append("\\r\\n"); string postHeader = sb.ToString(); byte\[\] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); // Build the trailing boundary string as a byte array // ensuring the boundary appears on a line by itself byte\[\] boundaryBytes = Encoding