private string perform_request() { Uri uri = new Uri(api_endpoint); string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri); webrequest.CookieContainer = new CookieContainer(); webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; webrequest.Timeout = 120000; //check whether it is file upload //TODO: Support batch requests string uploadfile = ""; if (request.RequestFields[0].ContainsKey("filepath")) {//get the file name to upload uploadfile = request.RequestFields[0]["filepath"].ToString(); request.RequestFields[0].Remove("filepath"); } //get JSON format string for 'data' parameter string data = request.GetEncoded(); //prepare header for HTTP POST StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("data"); sb.Append("\""); sb.Append("\r\n"); sb.Append("\r\n"); sb.Append(data); sb.Append("\r\n"); if (uploadfile != "") {//if createfile function, prepare 'filedata' field sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("filedata"); sb.Append("\""); sb.Append("\r\n"); sb.Append("\r\n"); } Stream requestStream = null; try { string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); FileStream fileStream = null; long length = 0; if (uploadfile != "") {//if file upload, find the length of the file fileStream = new FileStream(uploadfile, FileMode.Open, FileAccess.Read); length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; } else {//if no file upload, just get lenght of the 'data' field length = postHeaderBytes.Length; } webrequest.ContentLength = length; requestStream = webrequest.GetRequestStream(); // Write out our post header requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); if (uploadfile != "") {// if file upload, write out the file contents byte[] buffer = new Byte[checked ((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } // Write out the trailing boundary requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); } } finally { if (requestStream != null) { requestStream.Close(); } } // Read the response StreamReader reader = null; HttpWebResponse webResponse = (HttpWebResponse)webrequest.GetResponse(); try { // Read the responce reader = new StreamReader(webResponse.GetResponseStream()); if (webResponse.StatusCode == HttpStatusCode.OK) { // initiate new response object response = new APIResponse(); // decode response response.DecodeResponse(reader); if (this._init == 1) {//if it is first call, get the 'token' this.setSession(); } return(response.Status[0].Code); //TO DO: need to implement debug (to log.txt) } else { throw new Exception(this.internal_errors(2)); } } finally { if (webResponse != null) { webResponse.Close(); } } }