将数据发布到打开的URL而不登陆页面c#asp.net

时间:2018-11-27 22:03:36

标签: c# post form-submit postdata

我正在一个将数据发送到另一个网站的站点上,并且该站点具有使用URL的登录页面。问题是我不想执行重定向,而只是希望它将数据发送到该页面并继续其下的代码。
另一个站点没有API,我该怎么做?

我一直在查看所有示例,它们都是通过登录身份验证引用API url的,但是此URL不需要登录,只需使用 URL(www.example.come / submit)发送数据?Firstname = Firstname; LastName = LastName;) 这样的事情实际上并没有将页面重定向到该站点。

1 个答案:

答案 0 :(得分:1)

您可以使用WebRequest

  // 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 ();  

您可以了解有关此Here

的更多信息