示例#1
0
        /////////////////////////////////////////////////////////////////////////////////////////
        public static bool HttpPost(String strUrl, byte[] byteArray, int timeout, out String outResponse)
        {
            //*****parameters: timout in ms

            outResponse = "";


            //[rev]
            if (CString.StartWith(strUrl, "https"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
            }

            //-----Request
            // Create a request using a URL that can receive a post.
            WebRequest request;

            try {
                request = WebRequest.Create(strUrl);

                request.Timeout = timeout;

                // Set the Method property of the request to POST.
                request.Method = "POST";

                ////Add custom headers
                //if ( ccHeaders!=null ) {
                //    int c = ccHeaders.Count;
                //    String key, strValue;
                //    for (int i = 0; i<c; i++) {
                //        key = ccHeaders.Key(i);
                //        strValue = (String)ccHeaders.Item(i);
                //        request.Headers.Add(key, strValue);
                //    }
                //}

                // Set the ContentType property of the WebRequest.
                //request.ContentType = "application/x-www-form-urlencoded"
                //request.ContentType = "multipart/form-data"
                request.ContentType = "application/octet-stream";


                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
            }
            catch (Exception ex) {
                String msg = ex.Message;
                return(false);
            }

            //-----Stream
            // Get the request stream.
            Stream dataStream = null;

            try {
                dataStream = request.GetRequestStream();

                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);

                // Close the Stream object.
                dataStream.Close();
            }
            catch (Exception ex) {
                String msg = ex.Message;
                if (dataStream != null)
                {
                    dataStream.Close();
                }

                return(false);
            }

            //-----Response
            //Get the response.
            Stream       responseDataStream = null;
            WebResponse  response           = null;
            StreamReader reader             = null;

            try {
                response = request.GetResponse();

                // Display the status.
                //Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)

                // Get the stream containing content returned by the server.
                responseDataStream = response.GetResponseStream();

                // Open the stream using a StreamReader for easy access.
                reader = new StreamReader(responseDataStream);

                // Read the content.
                String responseFromServer = reader.ReadToEnd();

                // Display the content.
                //Console.WriteLine(responseFromServer)
                outResponse = responseFromServer;

                // Clean up the streams.
                reader.Close();
                responseDataStream.Close();
                response.Close();

                //OK
                return(true);
            }
            catch (Exception ex) {
                String msg = ex.Message;
                if (reader != null)
                {
                    reader.Close();
                }

                if (responseDataStream != null)
                {
                    responseDataStream.Close();
                }

                if (response != null)
                {
                    response.Close();
                }

                return(false);
            }
        }
示例#2
0
        ///////////////////////////////////////////////////////////////////////////////////////
        public static bool HttpGet(String strUrl, int timeout, out String outResponse)
        {
            //*****parameters: timout in ms
            outResponse = "";

            //-----Request
            // Initialize the WebRequest.
            System.Net.WebRequest myRequest;
            try {
                myRequest         = System.Net.WebRequest.Create(strUrl);
                myRequest.Timeout = timeout;
            }
            catch (Exception ex) {
                String msg = ex.Message;
                return(false);
            }


            //-----Response
            // return the response.
            System.Net.WebResponse myResponse = null;


            //[rev]
            if (CString.StartWith(strUrl, "https"))
            {
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
            }

            try {
                myResponse = myRequest.GetResponse();
                String strResponse = StreamToUTF8String(myResponse.GetResponseStream());

                outResponse = strResponse;

                // Code to use the WebResponse goes here.
                // Close the response to free resources.
                myResponse.Close();

                //OK
                return(true);
            }
            //catch (Exception ex) {
            //    String msg = ex.Message;
            //    if (myResponse !=null) {
            //        myResponse.Close();
            //    }

            //    return false;
            //}
            catch (WebException ex) {
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        outResponse = "NotFound";
                    }
                    else if (resp.StatusCode == HttpStatusCode.BadRequest)
                    {
                        outResponse = "BadRequest";
                    }
                    else
                    {
                        outResponse = "";
                    }
                }
                else
                {
                    outResponse = "";
                }

                //String msg = ex.Message;
                if (myResponse != null)
                {
                    myResponse.Close();
                }

                return(false);
            }
        }