zoukankan      html  css  js  c++  java
  • 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("
    --" + boundary + "
    "); 
                    var endBoundary = Encoding.ASCII.GetBytes("
    --" + boundary + "--
    "); 
                    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("
    --" + boundary + "
    ");
                        strBuf.Append("Content-Disposition: form-data; name="" + key + ""
    
    ");
                        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}"
    " +
                            "Content-Type: application/octet-stream
    
    ";
                        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();
                }
            }
    

      

  • 相关阅读:
    JS中的原型规则与原型链
    JS中的“==”与强制类型转换
    协作开发中常用的Git命令小结
    JavaScript变量类型检测总结
    IDEA IntelliJ常用设置以及快捷键(转)
    Spring 发送 Email
    SSM框架的整合思路&功能实现
    使用Eclipse把java文件打包成jar 含有第三方jar库的jar包
    基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表
    Volley源码学习笔记
  • 原文地址:https://www.cnblogs.com/hbsfgl/p/7517405.html
Copyright © 2011-2022 走看看