HttpWebRequest调用接口的方法两种写法



1.是不行的

 //第一种写法
                //Uri url = new Uri(Url);
                //byte[] reqbytes = new byte[file.InputStream.Length];// + 1]; // 上传文件字节数组大小
                //try
                //{
                //    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                //    req.Method = "POST";
                //    req.ContentType = "application/octet-stream";//"application/x-www-form-urlencoded";//
                //    req.ContentLength = reqbytes.Length;
                //    Stream stm = req.GetRequestStream();
                //    stm.Write(reqbytes, 0, reqbytes.Length);
                //    stm.Close();
                //    HttpWebResponse wr = (HttpWebResponse)req.GetResponse();//获取服务器反馈结果
                //    Stream stream = wr.GetResponseStream();
                //    stream.Write(boundarybytes, 0, boundarybytes.Length);

                //    StreamReader srd = new StreamReader(stream, Encoding.UTF8);
                //    PageStr += srd.ReadToEnd();//返回的结果,code:-1 msg:系统未接收到文件
                //    stream.Close();
                //    srd.Close();
                //}
                //catch (Exception e)
                //{
                //    PageStr += e.Message;
                //}


---------------------2.----下面可行----------------------------------------------------  

    ///


         ///
        ///

        ///
        /// NameValueCollection可为空
        ///
         public string PostFile(List files, NameValueCollection formDatas)
         {
             string returnValue = "";
             string url = "
             try
             {
                 var encoding = Encoding.UTF8;
                 string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                 byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
                 byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

                 //1.HttpWebRequest
                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//(this._url);
                 //request.Headers = this._header;
                 request.ContentType = "multipart/form-data; boundary=" + boundary;
                 request.Method = "POST";
                 request.KeepAlive = true;
                 request.Credentials = CredentialCache.DefaultCredentials;

                 using (Stream stream = request.GetRequestStream())
                 {
                     //1.1 keyalue
                     string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
                     if (formDatas != null)
                     {
                         foreach (string key in formDatas.Keys)
                         {
                             stream.Write(boundarybytes, 0, boundarybytes.Length);
                             string formitem = string.Format(formdataTemplate, key, formDatas[key]);
                             byte[] formitembytes = encoding.GetBytes(formitem);
                             stream.Write(formitembytes, 0, formitembytes.Length);
                         }
                     }

                     //1.2 file
                     string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
                     byte[] buffer = new byte[4096];
                     int bytesRead = 0;
                     foreach (var file in files)
                     {
                         stream.Write(boundarybytes, 0, boundarybytes.Length);
                         string header = string.Format(headerTemplate, file.formKey, file.fileName);
                         byte[] headerbytes = encoding.GetBytes(header);
                         stream.Write(headerbytes, 0, headerbytes.Length);
                         while ((bytesRead = file.fileStream.Read(buffer, 0, buffer.Length)) != 0)
                         {
                             stream.Write(buffer, 0, bytesRead);
                         }
                     }

                     //1.3 form end
                     stream.Write(endbytes, 0, endbytes.Length);
                 }
                 //2.WebResponse
                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                 using (StreamReader stream = new StreamReader(response.GetResponseStream()))
                 {
                     var result = stream.ReadToEnd();
                     if (string.IsNullOrEmpty(result))
                         returnValue = result;
                         //return returnValue  ;// HandleResult.Error("无返回数据!");
                 }
             }
             catch (Exception ex)
             {
                 ;// HandleResult.Error(ex.Message);
             }
             return returnValue;
         }


         ///


         /// 上传文件信息
         ///

         public class UploadFileInfo
         {
             ///
             /// Key
             ///

             public string formKey { get; set; }

             ///


             /// 文件名
             ///

             public string fileName { get; set; }

             ///


             /// 文件流
             ///

             public Stream fileStream { get; set; }

-----------------------------------------------------------------------------------------

这个确实复杂,因为往生很多代码看起来没错,但是,就不行。最后这个行了。。。。。服务器那边参数是HttpPostedFileBase----后续再改。
         }

你可能感兴趣的:(HttpWebRequest调用接口的方法两种写法)