WebRequest设置Header应该放最前面

WebRequest设置Header应该放最前面,如果先设置RequestBody,可能会破坏Header,造成服务器不能正确读取,即使跟踪查看header的数据都存在

public static byte[] request(string url, string method, ref Dictionary<string, string> header,object data = null, string encoding = "utf-8")
        {
            HttpWebResponse response = null;
            Stream stream = null;
            MemoryStream memoryStream = null;
            try
            {                
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method.ToUpper();

                if (header != null)
                {
                    foreach (string key in header.Keys)
                    {
                        request.Headers.Add(key, header[key]);
                    }
                }
                header = null;

                if (data != null)
                {
                    byte[] bytesRequestData = null;
                    if (data is Dictionary<string, object>)
                    {
                        if (request.ContentType == null)
                            request.ContentType = "application/x-www-form-urlencoded";
                        bytesRequestData = Encoding.Default.GetBytes(createQueryStringUrlEncode((Dictionary<string, object>)data, Encoding.GetEncoding(encoding)));
                    }
                    else if (data is string)
                    {
                        bytesRequestData = Encoding.Default.GetBytes(data as string);
                    }
                    else
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        memoryStream = new MemoryStream();
                        bf.Serialize(memoryStream, data);
                        bytesRequestData = memoryStream.GetBuffer();
                        memoryStream.Close();
                        memoryStream.Dispose();
                        memoryStream = null;
                    }
                    request.ContentLength = bytesRequestData.Length;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                    requestStream.Close();
                }

                response = (HttpWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                header = new Dictionary<string, string>();
                foreach (string key in response.Headers.Keys)
                {
                    header.Add(key, response.Headers[key]);
                }

                header["StatusCode"] = response.StatusCode.ToString();
                header["StatusDescription"] = response.StatusDescription;

                if(stream.CanRead)
                {
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    memoryStream = new MemoryStream();
                    while ((len = stream.Read(buffer, 0, 1024)) > 0)
                    {
                        memoryStream.Write(buffer, 0, len);
                    }
                    return memoryStream.GetBuffer();
                }
                return null;
            }catch(WebException ex){
                response = (HttpWebResponse)ex.Response;
                header = new Dictionary<string, string>();
                foreach (string key in response.Headers.Keys)
                {
                    header.Add(key, response.Headers[key]);
                }
                header["StatusCode"] = response.StatusCode.ToString();
                header["StatusDescription"] = response.StatusDescription;
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Close();
                    memoryStream.Dispose();
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
                if (response != null)
                {
                    response.Close();
                    response.Dispose();
                }
            }
        }

 

posted on 2016-01-15 16:27  Freewing  阅读(1281)  评论(0编辑  收藏  举报