public static PushResponse PushFile(string target, string path, DataUploadedListener listener) { NameValueCollection data = new NameValueCollection(); data.Add("device_id", target); data.Add("type", "file"); var res = HttpUploadFile("https://www.pushbullet.com/api/pushes", path, "file", "application/octet-stream", data, listener); if (res == null || res.Length == 0) throw new Exception("Got null from our request :("); var finalized = JsonConvert.DeserializeObject<PushResponse>(res); CheckForErrors(finalized); return finalized; }
public static PushResponse PushFile(string target, string path, DataUploadedListener listener) { NameValueCollection data = new NameValueCollection(); data.Add("device_id", target); data.Add("type", "file"); var res = HttpUploadFile("https://www.pushbullet.com/api/pushes", path, "file", "application/octet-stream", data, listener); if (res == null || res.Length == 0) { throw new Exception("Got null from our request :("); } var finalized = JsonConvert.DeserializeObject <PushResponse>(res); CheckForErrors(finalized); return(finalized); }
// performs an HTTP multipart request. Thanks to stackoverflow // http://stackoverflow.com/a/2996904 (slightly modified by adding a // data sent callback, chunked tranfer and authentication) private static string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc, DataUploadedListener listener) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(key + ":")); wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.Method = "POST"; wr.KeepAlive = true; wr.AllowWriteStreamBuffering = false; wr.SendChunked = true; //wr.Credentials = System.Net.CredentialCache.DefaultCredentials; Stream rs = wr.GetRequestStream(); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string k in nvc.Keys) { rs.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, k, nvc[k]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length); } rs.Write(boundarybytes, 0, boundarybytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, paramName, Path.GetFileName(file), contentType); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[4096]; long totalBytes = fileStream.Length; long sentBytes = 0; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { rs.Write(buffer, 0, bytesRead); sentBytes += bytesRead; if (listener != null) { if (!listener(sentBytes, totalBytes)) { throw new System.Threading.ThreadInterruptedException(); } } } fileStream.Close(); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); rs.Write(trailer, 0, trailer.Length); rs.Close(); WebResponse wresp = null; try { wresp = wr.GetResponse(); Stream stream2 = wresp.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); return(reader2.ReadToEnd()); } catch (Exception ex) { if (wresp != null) { wresp.Close(); wresp = null; } throw ex; } finally { wr = null; } }
// performs an HTTP multipart request. Thanks to stackoverflow // http://stackoverflow.com/a/2996904 (slightly modified by adding a // data sent callback, chunked tranfer and authentication) private static string HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc, DataUploadedListener listener) { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(url); wr.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(key + ":")); wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.Method = "POST"; wr.KeepAlive = true; wr.AllowWriteStreamBuffering = false; wr.SendChunked = true; //wr.Credentials = System.Net.CredentialCache.DefaultCredentials; Stream rs = wr.GetRequestStream(); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string k in nvc.Keys) { rs.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, k, nvc[k]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length); } rs.Write(boundarybytes, 0, boundarybytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, paramName, Path.GetFileName (file), contentType); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[4096]; long totalBytes = fileStream.Length; long sentBytes = 0; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { rs.Write(buffer, 0, bytesRead); sentBytes += bytesRead; if (listener != null) if (!listener(sentBytes, totalBytes)) throw new System.Threading.ThreadInterruptedException(); } fileStream.Close(); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); rs.Write(trailer, 0, trailer.Length); rs.Close(); WebResponse wresp = null; try { wresp = wr.GetResponse(); Stream stream2 = wresp.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); return reader2.ReadToEnd(); } catch (Exception ex) { if (wresp != null) { wresp.Close(); wresp = null; } throw ex; } finally { wr = null; } }