Exemple #1
0
        private static string _commonHttpRequest(string url, string type, ContentType eContentType, string data = "")
        {
            HttpWebRequest  myRequest  = null;
            Stream          outstream  = null;
            HttpWebResponse myResponse = null;
            StreamReader    reader     = null;

            try
            {
                //构造http请求的对象
                myRequest = (HttpWebRequest)WebRequest.Create(url);
                //设置
                myRequest.ProtocolVersion = HttpVersion.Version10;
                myRequest.Method          = type;

                if (data.Trim() != "")
                {
                    if (eContentType == ContentType.FORM)
                    {
                        myRequest.ContentType = "application/x-www-form-urlencoded";
                    }
                    else if (eContentType == ContentType.JSON)
                    {
                        myRequest.ContentType = "application/json";
                    }
                    else
                    {
                        myRequest.ContentType = "text/xml";
                    }

                    //转成网络流
                    byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(data);

                    myRequest.ContentLength = buf.Length;
                    //myRequest.Headers.Add("data", data);

                    outstream = myRequest.GetRequestStream();
                    outstream.Flush();
                    outstream.Write(buf, 0, buf.Length);
                    outstream.Flush();
                    outstream.Close();
                }
                // 获得接口返回值
                myResponse = (HttpWebResponse)myRequest.GetResponse();
                reader     = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string ReturnXml = reader.ReadToEnd();
                reader.Close();
                myResponse.Close();
                myRequest.Abort();
                return(ReturnXml);
            }
            catch (Exception ex)
            {
                SysLogUtils.error4Sys(LOG_COMMONHTTPREQUEST, ex.Message);
                if (outstream != null)
                {
                    outstream.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
                if (myResponse != null)
                {
                    myResponse.Close();
                }
                if (myRequest != null)
                {
                    myRequest.Abort();
                }
                return("");
            }
        }
Exemple #2
0
        public static string uploadFat(string uri, string filePath, object obj,
                                       string fileKeyName = "file", int timeOut = 3600000)
        {
            string          result  = "";
            FormDataUtilsEx form    = new FormDataUtilsEx();
            HttpWebRequest  request = (HttpWebRequest)WebRequest.Create(uri);

            request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
            request.Method      = "Post";
            //request.AllowWriteStreamBuffering = false;
            request.Timeout = timeOut;

            //form data
            form.AddFormField(obj);
            string fileName = Path.GetFileName(filePath);

            form.AddStreamFile(fileKeyName, fileName);

            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            //二进制对象
            BinaryReader r = new BinaryReader(file);

            try
            {
                Stream stream = request.GetRequestStream();
                foreach (var b in form.GetFormData())
                {
                    stream.WriteByte(b);
                }
                //每次上传4k
                int    bufferLength = 4096;
                byte[] buffer       = new byte[bufferLength]; //已上传的字节数
                long   offset       = 0;                      //开始上传时间
                int    size         = r.Read(buffer, 0, bufferLength);
                long   length       = file.Length;
                while (size > 0)
                {
                    stream.Write(buffer, 0, size);
                    offset += size;
                    Application.DoEvents();
                    size = r.Read(buffer, 0, bufferLength);
                }

                foreach (var b in form.getSuffix())
                {
                    stream.WriteByte(b);
                }
                stream.Close();

                WebResponse response = request.GetResponse();

                using (var httpStreamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    result = httpStreamReader.ReadToEnd();
                }
                response.Close();
                request.Abort();
                return(result);
            }
            catch (Exception ex)
            {
                SysLogUtils.error4Sys(LOG_UPLOADFAT, ex.Message);
                return("");
            }
            finally
            {
                file.Close();
                r.Close();
            }
        }