SendRequest

        public static string SendRequest(string url, SortedDictionary<string, object> requestParams, string requestMethod, string fileName)
        {
            if (requestMethod == "GET")
            {
                var paramStr = "";
                foreach (var key in requestParams.Keys)
                {
                    paramStr += string.Format("{0}={1}&", key, HttpUtility.UrlEncode(requestParams[key].ToString())); 
                }
                paramStr = paramStr.TrimEnd('&');
                url += (url.EndsWith("?") ? "&" : "?") + paramStr;
            }

            var request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Accept = "*/*";
            request.KeepAlive = true;
            request.Timeout = timeOut;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
            if (requestMethod == "POST")
            {
                var boundary = "---------------" + DateTime.Now.Ticks.ToString("x"); 
                var beginBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); 
                var endBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); 
                request.Method = requestMethod;
                request.ContentType = "multipart/form-data; boundary=" + boundary;

                var memStream = new MemoryStream();

                var strBuf = new StringBuilder();
                foreach(var key in requestParams.Keys)
                {
                    strBuf.Append("\r\n--" + boundary + "\r\n");
                    strBuf.Append("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n");
                    strBuf.Append(requestParams[key].ToString());
                }
                var paramsByte = Encoding.GetEncoding("utf-8").GetBytes(strBuf.ToString());
                memStream.Write(paramsByte, 0, paramsByte.Length);

                if (fileName != null)
                {
                    memStream.Write(beginBoundary, 0, beginBoundary.Length);
                    var fileInfo = new FileInfo(fileName);
                    var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
             
                    const string filePartHeader = 
                        "Content-Disposition: form-data; name=\"entityFile\"; filename=\"{0}\"\r\n" +
                        "Content-Type: application/octet-stream\r\n\r\n";
                    var header = string.Format(filePartHeader, fileInfo.Name);
                    var headerbytes = Encoding.UTF8.GetBytes(header);
                    memStream.Write(headerbytes, 0, headerbytes.Length);

                    var buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        memStream.Write(buffer, 0, bytesRead);
                    }  
                }
                memStream.Write(endBoundary, 0, endBoundary.Length);
                request.ContentLength = memStream.Length;

                var requestStream = request.GetRequestStream();

                memStream.Position = 0;
                var tempBuffer = new byte[memStream.Length];
                memStream.Read(tempBuffer, 0, tempBuffer.Length);
                memStream.Close();

                requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                requestStream.Close();  
            }

            var response = request.GetResponse();
            using (var s = response.GetResponseStream())
            {
                var reader = new StreamReader(s, Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }

  

posted @ 2017-09-13 19:49  hbsfgl  阅读(1289)  评论(0编辑  收藏  举报