public string WebRequestWithFileStream(Method method, string url, string postData, OAuthBase oauth, Stream fileStream) { var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest; if (webRequest == null) { return(string.Empty); } webRequest.Method = method.ToString(); webRequest.ServicePoint.Expect100Continue = false; webRequest.UserAgent = "WinWeiBee"; webRequest.Timeout = 20000; webRequest.KeepAlive = true; NameValueCollection qs = HttpUtility.ParseQueryString(postData); XAuthHeader(webRequest, qs); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); byte[] boundarybytes1 = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); webRequest.ContentType = "multipart/form-data;boundary=" + boundary; Stream requestStream = webRequest.GetRequestStream(); const string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; MultipartformBody(qs, boundarybytes, boundarybytes1, requestStream, formdataTemplate); // Write file type to head const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, "pic", "pic.jpg", Jpeg); byte[] headerbytes = Encoding.UTF8.GetBytes(header); requestStream.Write(headerbytes, 0, headerbytes.Length); // Write picture file binary to post data FileStreamToRequestStream(requestStream, fileStream); // The trailer data byte[] trailer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(trailer, 0, trailer.Length); requestStream.Close(); return(Utility.WebResponseGet(webRequest)); //, url, postData); }
private string WebRequestWithFile(Method method, string url, string postData, string filePath, OAuthBase oauth) { var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); string responseData = WebRequestWithFileStream(method, url, postData, oauth, fileStream); fileStream.Close(); return(responseData); }