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

问题描述 投票:1回答:1

我正在开发一个将数据发送到另一个网站的网站,它有一个带有URL的登录页面。问题是我不想做REdirect我只是希望它将数据发送到该页面并继续下面的代码。 另一个站点没有API我该怎么办?

我一直在查看示例,所有这些示例都指的是具有登录身份验证的API网址,但此URL不需要登录,只需使用URL发送数据(www.example.come / submit?Firstname = Firstname; LastName = LastName;)类似的东西没有实际将页面重定向到该站点。

c# post form-submit postdata
1个回答
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的信息

© www.soinside.com 2019 - 2024. All rights reserved.