当前位置:编程学习 > C#/ASP.NET >>

求助攻 C# post请求出错

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication3
{
    public class Program
    {
        public static void Main(string[] args)
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("Connection", "keep-alive");
            nvc.Add("Content-Length", "69140");
            nvc.Add("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySBA0xqGYm55HNikN");
            nvc.Add("Accept-Encoding", "gzip,deflate,sdch");
            string[] filenames = new string[] { @"C:\\Users\\MHapDream\\Pictures\\123.jpg" };
            UploadFilesToRemoteUrl(@"http://121.199.45.245/shanshangzx/upload", filenames, nvc);
            Console.ReadKey();
        }

        public static void UploadFilesToRemoteUrl(string url, string[] files, NameValueCollection nvc)
        {

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

            HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest2.ContentType = "multipart/form-data; boundary=" +
            boundary;
            httpWebRequest2.Method = "POST";
            httpWebRequest2.KeepAlive = true;
            httpWebRequest2.Credentials =
            System.Net.CredentialCache.DefaultCredentials;

            Stream memStream = new System.IO.MemoryStream();

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

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

            foreach (string key in nvc.Keys)
            {
                string formitem = string.Format(formdataTemplate, key, nvc[key]);
                byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }

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

            string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";

            for (int i = 0; i < files.Length; i++)
            {

                //string header = string.Format(headerTemplate, "file" + i, files[i]);
                string header = string.Format(headerTemplate, "uplTheFile", files[i]);

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

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

                FileStream fileStream = new FileStream(files[i], FileMode.Open,
                FileAccess.Read);
                byte[] buffer = new byte[1024];

                int bytesRead = 0;

                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    memStream.Write(buffer, 0, bytesRead);

                }

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

                fileStream.Close();
            }

            httpWebRequest2.ContentLength = memStream.Length;

            Stream requestStream = httpWebRequest2.GetRequestStream();

            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();
            WebResponse webResponse2 = null;
            try
            {
                webResponse2 = httpWebRequest2.GetResponse();
            }
            catch (WebException e)
            {
                Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
            }
            Console.ReadKey();
            Stream stream2 = webResponse2.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);

            webResponse2.Close();
            httpWebRequest2 = null;
            webResponse2 = null;
        }
    }
}

错误 --------------------编程问答-------------------- 挽尊 --------------------编程问答-------------------- 这说明服务器产生了内部错误。既有可能是你上传的数据本身有问题,也可能是他服务器端的程序有bug。如果你的代码是复制粘贴来的,那么是它自身的问题可能比较大。 --------------------编程问答-------------------- 这么写太累了,哪里错了都不知道,查查 HttpWebRequest 的用法吧。 --------------------编程问答-------------------- 在构建HTTP协议好像有错误,没有结束标记
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,