首页 新闻 会员 周边

c# 通过post数据到某一网页,如何在服务器端获取该post数据

0
[已解决问题] 解决于 2017-03-27 13:17

            // Create a request using a URL that can receive a post.   
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:56624/Default2.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 = "multipart/form-data";
            // 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.  
            HttpWebResponse response = (HttpWebResponse)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();

 

 

通过上面代码把数据post到某一网页,如何获取该Post数据呢

在线等,万分感谢

beautifulday的主页 beautifulday | 初学一级 | 园豆:190
提问于:2017-03-17 13:04
< >
分享
最佳答案
0

在Default2.aspx的Page_Load中  

string str="";
 using (StreamReader sr=new StreamReader(Request.InputStream)) {
              str=  sr.ReadToEnd();
            }
奖励园豆:5
孤零落叶寒 | 小虾三级 |园豆:606 | 2017-03-18 09:06
其他回答(2)
0

post到网页,感觉有点不对吧,网页上又没有接收这些数据的方法。post应该是直接post到服务端的某个方法上了吧。

开山怪不怪 | 园豆:544 (小虾三级) | 2017-03-17 15:33
0

在 http://localhost:56624/  这个 站点的 Default2.aspx 页面  监视下  Request  对象 里面有什么就知道了  

pengbg | 园豆:13 (初学一级) | 2017-03-21 15:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册