How to Post Data to a website through win forms c#

14,155

Solution 1

Yes , you can use WebClient class.

public static string PostMessageToURL(string url, string parameters)
{
    using (WebClient wc = new WebClient())
    {
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        string HtmlResult = wc.UploadString(url,"POST", parameters);
        return HtmlResult;
    }
}

Example:

PostMessageToURL("http://tempurl.org","query=param1&query2=param2");

Solution 2

Absolutely, take a look at WebRequest, here is a full example

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

You can then do this kind of thing

UriBuilder uriBuilder = new UriBuilder(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriBuilder.Uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytesToPost.Length;

using(Stream postStream = request.GetRequestStream())
{
     postStream.Write(bytesToPost, 0, bytesToPost.Length);
     postStream.Close();
}

HttpWebResponse response = (HttpWebResponse )request.GetResponse();
string url = response.ResponseUri

and the last line will give you the URL (success/fail) you are after

Share:
14,155

Related videos on Youtube

Pavitar
Author by

Pavitar

I have pursued a Diploma in Software Development , in which I have learnt multiple languages. I'm also a photography enthusiast,as it runs in the Family :) Interests: Java, Asp.Net, PHP, Angular, NodeJS, Vuejs, Embedded-C, IOT

Updated on September 15, 2022

Comments

  • Pavitar
    Pavitar over 1 year

    I have an api where in I can post some data n submit and then get whether posted data is valid or no. This api redirects to different urls indicating sucess/failure. For ths what I normally do is , within html tags call the destination url and submit the page:

        <form method="post" action="https://web.tie.org/verify.php" name="main">
    <table width="100%" border="0" cellpadding="0" cellspacing="0" align="center" valign="top">
        <tr>
            <td class="normal">&nbsp;</td><td class="normal"><input type='text' class='text' name='Email' value='[email protected]' size='15' maxlength='35'></td>
        </tr>
    </table>
    </form>
    <script language='javascript'>
    
    document.forms[0].submit();
    
    </script>
    

    Is there a way to post data directly through winforms c#. I want to be able to access the success/failure url after post and get the query string of the redirected site.

    With Reference to enter link description here I have tried posting but I need the result query string.

    Right now I can achieve this by:

    webBrowser1.Url = new Uri("C:\\Documents and Settings\\Admin\\Desktop\\calltie.html");            
    webBrowser1.Show();
    
  • Peyman Mehrabani
    Peyman Mehrabani over 11 years
    PostMessageToURL method return the result :)
  • Pavitar
    Pavitar over 11 years
    My PostMessageToURL("tempurl.org","query=param1&query2=param2")‌​; in turn redirects to: query=param1 and appends to that query string. I need the redirected querys tring to pull out activated status. For eg. on sucess it will redirect to param1?valid=yes&exp=a . I want to know what is valid and exp equalt to
  • Justin Harvey
    Justin Harvey over 11 years
    @Pavitar, see my updated answer below, you need to pull out the responseUri. I am not sure WebClient gives you enough detail to read the headers like this.
  • Peyman Mehrabani
    Peyman Mehrabani over 11 years
    It's depends on how requested url redirect to new page.by calling javascript function? 304 method o set location to header?
  • Pavitar
    Pavitar over 11 years
    This FieldInfo responseField = webclient.GetType().GetField("m_WebResponse", BindingFlags.Instance | BindingFlags.NonPublic); did the trick. :)