Web服务上载文件错误

时间:2011-07-06 14:26:32

标签: c# web-services

我正在将文件上传到网络服务......网络服务的协议如下:

HTTP POST请求参数“USERNAME”,“PASSWORD”和“FILE”是必需的。

必须使用多部分表单提交,即使用'multipart / form-data'的FORM'enctype'属性。

和 当用于构造URL时,“&”字符应替换为“%26”,“”(空格)替换为“%20”。

我指的是帖子Upload files with HTTPWebrequest (multipart/form-data)来构建此网络请求...

我的代码如下......

string boundary =“---------------------------”+ DateTime.Now.Ticks.ToString(“x”); < / p>

            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);

            wr.ContentType = "multipart/form-data; boundary=" + boundary;
            wr.Method = "POST";
            Stream rs = wr.GetRequestStream();

            string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";



            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("USERNAME", "myusernammeee");

// nvc.Add(“PASSWORD”,“mS89_n3w”);

            nvc.Add("PASSWORD", "mypasswordddd");

            foreach (string key in nvc.Keys)
            {

                rs.Write(boundarybytes, 0, boundarybytes.Length);

                string formitem = string.Format(formdataTemplate, key, nvc[key]);

                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);

                rs.Write(formitembytes, 0, formitembytes.Length);
            }

            rs.Write(boundarybytes, 0, boundarybytes.Length);

            string headerTemplate =
                "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";

            string header = string.Format(headerTemplate, "FILE", files[0], "xml");

            byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

            rs.Write(headerbytes, 0, headerbytes.Length);

            StringBuilder inFile = new StringBuilder(1000);
            //First of all lets read the contents of the file in.
            using (TextReader streamReader = new StreamReader(files[0]))
            {
                while (streamReader.Peek() >= 0)
                {
                    inFile.Append(streamReader.ReadLine());
                }
            }
            string inputFile = inFile.ToString().Replace("&amp;", "%26amp;");

            byte[] fileBytes = Encoding.UTF8.GetBytes(inputFile);

            rs.Write(fileBytes,0, fileBytes.Length);

            byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

            rs.Write(trailer, 0, trailer.Length);

            rs.Close();

            WebResponse wresp = null;

            try
            {

                wresp = wr.GetResponse();

                Stream stream2 = wresp.GetResponseStream();

                StreamReader reader2 = new StreamReader(stream2);

            }
            catch (Exception ex)
            {

                if (wresp != null)
                {

                    wresp.Close();

                    wresp = null;
                }
            }
            finally
            {
                wr = null;
            }

但是当我这样做时,我收到错误“错误请求”。我不确定我在这里做错了什么..任何建议都将不胜感激。

谢谢

0 个答案:

没有答案