Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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();
like image 357
Pavitar Avatar asked Dec 17 '12 11:12

Pavitar


1 Answers

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");
like image 142
Peyman Mehrabani Avatar answered Sep 22 '22 07:09

Peyman Mehrabani