使用WebRequest的http post请求

时间:2013-04-03 23:59:33

标签: c#

//来自http://msdn.microsoft.com/en-us/library/debx8sh9.aspx 我POST的表单使用fiddler工具返回以下字符串,该工具运行正常。 的 学期= 20143Fall + 2013 +++++++++++++++++++++++++++++++&安培; courseid =安培;受试者= IT ++信息+技术与安培;大学=安培;校园= 1%2C2%2C3%2C4%2C5%2C6%2C7%2C9%2CA%2CB%2CC%2CI%2CL%2CM%2CN%2CP%2CQ%2CR%2CS %2CT%2CW%2CU%2CV%2CX%2CZ&安培; courselevel =安培; coursenum =安培; STARTTIME = 0600&安培;结束时间= 2359&安培;天= ALL&安培;所有=所有+节

我要做的是让我的应用程序从下拉列表中传入该字符串的值。我有一个ddl名称ddlSemester和2013年秋季例如具有值“20143Fall + 2013 ++++++++++++++++++++++++++++++++++++ 。我想用ddl添加它。我已经尝试了ddlSemester.SelectedIndex和ddlSemester.SelectedValue,到目前为止没有运气。 这是我遇到问题的部分 string postData =“这是一个将此字符串发布到Web服务器的测试。”;

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main ()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes (postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream ();
            // Write the data to the request stream.
            dataStream.Write (byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close ();
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}

0 个答案:

没有答案