POST本机没问题。但是POST到其他服务器就有问题了。
我用以下的方式接受POST请求,但是发送的程序总是报错
错误是
远程服务器返回错误:(500)内部服务器错误
请问这该怎么解决 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;namespace WindowsApplication105
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        string UrlPost = "http://cangxuan.wordpress.com.cn/wp-comments-post.php";
        string strReferer = "http://cangxuan.wordpress.com.cn/2009/04/21/hello-world/";
        string strArgs = "author=xxxx&[email protected]&url=http://adfas.com&comment=TestTestTestTestTest&submit=Submit+Comment&comment_post_ID=1&comment_parent=0&wphc_value=42844036";        public static string PostLogin(string UrlPost, string strArgs, string strReferer)
        {
            string strResult = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(UrlPost);
            req.AllowAutoRedirect = true;
            req.KeepAlive = true;
            req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            req.Referer = strReferer;
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; Foxy/1; .NET CLR 2.0.50727; MAXTHON 2.0)";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";            CookieCollection myCookies = null;
            CookieContainer myCookieContainer = new CookieContainer();
            req.CookieContainer = myCookieContainer;            Stream MyRequestStream = req.GetRequestStream();
            StreamWriter sw = new StreamWriter(MyRequestStream, Encoding.ASCII);            //把数据写入HttpWebRequest的Request流
            sw.Write(strArgs);
            //关闭打开对象
            sw.Close();
            MyRequestStream.Close();            //下面这步就开始返回   远程服务器返回错误:(500)内部服务器错误
            HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
            string cookieHeader = req.CookieContainer.GetCookieHeader(new Uri(UrlPost));
            HttpContext.Current.Application.Lock();
            HttpContext.Current.Application["cookieHeader"] = cookieHeader;
            HttpContext.Current.Application.UnLock();
            myCookies = rep.Cookies;            StreamReader sr = new StreamReader(rep.GetResponseStream(), Encoding.UTF8);
            strResult = sr.ReadToEnd();
            return strResult;
        }        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = PostLogin(UrlPost, strArgs, strReferer);
        }
    }
}

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;
    namespace testpost
    {
        class Program
        {
            static string UrlPost = "http://cangxuan.wordpress.com.cn/wp-comments-post.php";
            static string strReferer = "http://cangxuan.wordpress.com.cn/2009/04/21/hello-world/";
            static string strArgs = "author=xxx&comment=特色他&comment_parent=0comment_post_ID=1&[email protected]&submit=Submit Comment&url&http://www.sohu.com&wphc_value=42844036";
            static void Main(string[] args)
            {
                            string strResult = "";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(UrlPost);
                req.AllowAutoRedirect = true;
                req.KeepAlive = true;
                req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8";
                req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                req.Referer = strReferer;
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                byte[] byteArray = Encoding.UTF8.GetBytes(strArgs);
                req.ContentLength = byteArray.Length;            CookieCollection myCookies = null;
                CookieContainer myCookieContainer = new CookieContainer();
                req.CookieContainer = myCookieContainer;            Stream MyRequestStream = req.GetRequestStream();
                           //把数据写入HttpWebRequest的Request流
              
               
                MyRequestStream.Write(byteArray,0,byteArray.Length);
                //关闭打开对象
               
                //MyRequestStream.Close();            //下面这步就开始返回  远程服务器返回错误:(500)内部服务器错误
                HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
                string cookieHeader = req.CookieContainer.GetCookieHeader(new Uri(UrlPost));
                StreamReader sr = new StreamReader(rep.GetResponseStream(), Encoding.UTF8);
                strResult = sr.ReadToEnd();        }   
        }
    }