Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill forms and submit with Webclient in C#

Tags:

c#

webclient

You should probably be using HttpWebRequest for this. Here's a simple example:

var strId = UserId_TextBox.Text;
var strName = Name_TextBox.Text;

var encoding=new ASCIIEncoding();
var postData="userid="+strId;
postData += ("&username="+strName);
byte[]  data = encoding.GetBytes(postData);

var myRequest =
  (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
var newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

var response = myRequest.GetResponse();
var responseStream = response.GetResponseStream();
var responseReader = new StreamReader(responseStream);
var result = responseReader.ReadToEnd();

responseReader.Close();
response.Close();

Try this:

using System.Net;
using System.Collections.Specialized;  

NameValueCollection values = new NameValueCollection();
values.Add("TextBox1", "value1");
values.Add("TextBox2", "value2");
values.Add("TextBox3", "value3");
string Url = urlvalue.ToLower();

using (WebClient client = new WebClient())
{
    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    byte[] result = client.UploadValues(Url, "POST", values);
    string ResultAuthTicket = System.Text.Encoding.UTF8.GetString(result);
}

I have found a solution to my problem. First of all I was confused about some of the basics in http-communication. This was caused by a python-script i wrote, which have a different approach with the communication.

I solved it with generating the POST-data from scratch and open the uri, which was contained in the form-action.


BFree's answer works great. One thing I would note, though, is that the data concatenation should really be url-encoded, otherwise you'd have trouble with things like "=" and "&" signs within the data.

The VB.NET version, urlencoded and with UTF-8 support, is below (note that url-encoding requires a reference to System.Web.dll, which only worked for me after I switched from .NET 4 Compact Framework to the regular .NET 4 Framework).

Imports System.Web
Imports System.Net
Imports System.IO

Public Class WebFormSubmitter

    Public Shared Function submit(ByVal address As String,
                                  ByVal values As Dictionary(Of String, String)) As String
        Dim encoding As New UTF8Encoding
        Dim postData As String = getPostData(values:=values)
        Dim data() As Byte = encoding.GetBytes(postData)

        Dim request = CType(WebRequest.Create(address), HttpWebRequest)
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = data.Length
        Dim newStream = request.GetRequestStream()
        newStream.Write(data, 0, data.Length)
        newStream.Close()

        Dim response = request.GetResponse()
        Dim responseStream = response.GetResponseStream()
        Dim responseReader = New StreamReader(responseStream)
        Return responseReader.ReadToEnd()
    End Function

    Private Shared Function getPostData(ByVal values As Dictionary(Of String, String)) As String
        Dim postDataPairList As New List(Of String)
        For Each anEntry In values
            postDataPairList.Add(anEntry.Key & "=" & HttpUtility.UrlEncode(anEntry.Value))
        Next
        Return String.Join(separator:="&", values:=postDataPairList)
    End Function

End Class

Posting a form with System.Net.Http.HttpClient and reading the response as string:

var formData = new Dictionary<string, string>();
formData.Add("number", number);

var content = new FormUrlEncodedContent(formData);

using (var httpClient = new HttpClient())
{
    var httpResponse = await httpClient.PostAsync(theurl, content);

    var responseString = await httpResponse.Content.ReadAsStringAsync();
}