开发者

Web service upload file errors

I am uploading a file to a web service...the prootocol for the web service is like this :

HTTP POST request parameters “USERNAME” , “PASSWORD” and “FILE” is required.

Multipart form submission, i.e. use a FORM 'enctype' attribute of 'multipart/form-data', must be used.

and the “&” character should be replaced by “%26”, and “ “ (space) replaced by “%20” when used to construct a URL.

I am referring to the post Upload files with HTTPWebrequest (multipart/form-data) to build this web request...

my code is as follows...

string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

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

            HttpWebRequest wr = (HttpWe开发者_JAVA百科bRequest)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("&", "%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;
            }

but when i do this I am getting an error "Bad Request". I am not sure what I am doing wrong here.. Any suggestion would be greatly appreciated.

Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜