Пример #1
0
        public PostWebServiceResponse PostAsync <T>(T item)
        {
            PostWebServiceResponse response = new PostWebServiceResponse();

            try
            {
                WebRequest req = HttpWebRequest.Create(RequestUrl);
                req.Method = MethodType;


                if (Headers.Count > 0)
                {
                    BuildHeaders(req);
                }
                Body = JsonConvert.SerializeObject(item);
                Stream       stream = req.GetRequestStream();
                StreamWriter wr     = new StreamWriter(stream);
                wr.Write(Body);
                wr.Flush();
                wr.Close();
                stream.Close();
                //req.ContentLength = stream.Length;

                var          r      = req.GetResponse();
                Stream       s      = r.GetResponseStream();
                StreamReader reader = new StreamReader(s, true);
                string       x      = reader.ReadToEnd();

                var settings = new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                };
                if (Resolver != null)
                {
                    settings.ContractResolver = Resolver;
                }


                response = JsonConvert.DeserializeObject <PostWebServiceResponse>(x, settings);
                return(response);
            }
            catch (Exception ex)
            {
                string exception = BuildLogMessage() + " : " + ex.Message;
                LoggingFactory.GetLogger().Log(exception);
                response.statusId = "0";
                return(response);
            }
        }
Пример #2
0
        public PostWebServiceResponse PostAsyncWithBinary <T>(T item, string fileName, byte[] buffer)
        {
            PostWebServiceResponse response = new PostWebServiceResponse();

            try
            {
                //  HttpClient client = new HttpClient();

                //Defining the unique boundary
                string     boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
                WebRequest req      = HttpWebRequest.Create(RequestUrl);
                if (Headers.Count > 0)
                {
                    BuildHeaders(req);
                }
                req.Method      = MethodType;//Post
                req.ContentType = "multipart/form-data; boundary=" + boundary;
                Stream stream = req.GetRequestStream();


                //Body need to be extended for each part of the request
                // Add header for JSON part


                // Body += "Content-Disposition: form-data; name='entity'\r\n";//entity is relative to the object we r sending
                //Body += "Content-Type: application/json\r\n\r\n";//defining the content type for this part of request
                // Add document object data in JSON
                //Body += JsonConvert.SerializeObject(item);
                Body += "\r\n--" + boundary + "\r\n";


                // Body += "Content-Disposition: form-data; name='record'\r\n";
                //  Body += "Content-Type: application/json\r\n\r\n";

                Body += string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n", "record");

                string jsonString = JsonConvert.SerializeObject(item);
                Body += jsonString;

                //Finalized the json part



                if (buffer != null)
                {
                    //Now we need to add the header for the binary part inside the body
                    Body += "\r\n--" + boundary + "\r\n";
                    Body += "Content-Disposition: form-data; name=\"picture\"; filename=\"" + fileName + "\"\r\n";
                    Body += "Content-Type: application/octet-stream\r\n\r\n";


                    // Body += "Content-Type: image/png\r\n\r\n";
                }


                //Now we need  to write the headers to the request
                // Add header data to request
                byte[] data = System.Text.Encoding.UTF8.GetBytes(Body);
                stream.Write(data, 0, data.Length);

                // Add binary file to request

                if (buffer != null)
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
                // Finalizing by adding the footer of the request or what we call trailer
                byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                stream.Write(trailer, 0, trailer.Length);
                stream.Close();


                // Do the post and get the response.

                var          r      = req.GetResponse();
                Stream       s      = r.GetResponseStream();
                StreamReader reader = new StreamReader(s, true);
                string       x      = reader.ReadToEnd();

                var settings = new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                };
                if (Resolver != null)
                {
                    settings.ContractResolver = Resolver;
                }


                response = JsonConvert.DeserializeObject <PostWebServiceResponse>(x, settings);
                return(response);
            }
            catch (Exception ex)
            {
                string exception = BuildLogMessage() + " : " + ex.Message;
                LoggingFactory.GetLogger().Log(exception);
                response.statusId = "0";
                return(response);
            }
        }