解决方案 »

  1.   

    string UpLoadPhoto(string filePath, string filename, string remotefilename, string httpServerIP, string httpUserID, string httpPassword)
            {
                string ret = string.Empty;
                FileInfo fileInf = new FileInfo(filePath + "\\" + filename);
                string uri = httpServerIP;
                HttpWebRequest reqHttp = (HttpWebRequest)HttpWebRequest.Create(new Uri(uri));
                try
                {                // 设置身份认证            
                    reqHttp.Credentials = new NetworkCredential(httpUserID, httpPassword);
                    //是否与 Internet 资源建立持久性连接 false为否          
                    reqHttp.KeepAlive = true;
                    //改为以Post方式提交
                    reqHttp.Method = "POST";
                    
                    //reqHttp.ContentType = "application/x-www-form-urlencoded";
                    string boundary = "cH2ei4gL6cH2GI3ei4KM7KM7Ij5gL6"; //创建边界码
                    reqHttp.ContentType = "multipart/form-data; boundary=----------" + boundary; //设置Content-Type标头的值                reqHttp.Accept = "*/*";
                    reqHttp.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
                    //创建请求实体第一部分
                    StringBuilder reqBody1 = new StringBuilder("\r\n");
                    reqBody1.AppendLine(string.Format("{0}{1}", "------------", boundary));
                    reqBody1.AppendLine(string.Format("{0}", "Content-Disposition: form-data; name=\"Filename\""));
                    reqBody1.AppendLine();
                    reqBody1.AppendLine(string.Format("{0}", filename));
                    reqBody1.AppendLine(string.Format("{0}{1}", "------------", boundary));
                    reqBody1.AppendLine(string.Format("Content-Disposition: form-data; name=\"Filedata\"; filename=\"{0}\"", filename));
                    reqBody1.AppendLine(string.Format("nContent-Type: application/octet-stream"));
                    reqBody1.AppendLine();                //将请求第一部分转换成字节序列
                    var reqBody1Bytes = Encoding.UTF8.GetBytes(reqBody1.ToString());                int buffLength = (Int32)fileInf.Length;
                    var reqBody2Bytes = new byte[buffLength];
                  
                    FileStream fs = new FileStream(filePath + "\\" + filename, FileMode.Open);                fs.Read(reqBody2Bytes, 0, buffLength);
                    fileInf = null;
                    fs = null;                //创建请求实体第三部分
                    StringBuilder reqBody3 = new StringBuilder();
                   
                    reqBody3.AppendLine();
                    
                    reqBody3.AppendLine(string.Format("------------{0}--", boundary));                //将请求第三部分转换成字节序列
                    var reqBody3Bytes = Encoding.UTF8.GetBytes(reqBody3.ToString());                //设置Content-Length标头
                    reqHttp.ContentLength = reqBody1Bytes.Length + reqBody2Bytes.Length + reqBody3Bytes.Length;
                    //创建请求写入流,并将主体的各部分的字节序列写入http流
                    Stream writer = reqHttp.GetRequestStream();
                    writer.Write(reqBody1Bytes, 0, reqBody1Bytes.Length);
                    writer.Write(reqBody2Bytes, 0, reqBody2Bytes.Length);
                    writer.Write(reqBody3Bytes, 0, reqBody3Bytes.Length);        
                    
                    // 获取提交数据后返回的信息 
                    using (HttpWebResponse response = (HttpWebResponse)reqHttp.GetResponse())
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new Exception("返回失败");
                        }
                        StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                        ret = sr.ReadToEnd();                }
                    return ret;
                }
                catch (Exception ex)
                {
                    reqHttp.Abort();
                    Console.WriteLine(ex.Message);
                    ret = ex.Message.ToString();
                    return ret;
                }
            }
      

  2.   

    reqHttp.ContentType = "application/x-www-form-urlencoded";==> reqHttp.ContentType = "multipart/form-data";