如何通过win表单将数据发布到网站c#

Pav*_*tar 6 c# winforms

我有一个api在哪里我可以发布一些数据n提交,然后得到发布的数据是有效还是没有.这个api重定向到不同的URL表示成功/失败.对于我通常做的是,在html标签内调用目标网址并提交页面:

    <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='x@hotmail.com' size='15' maxlength='35'></td>
    </tr>
</table>
</form>
<script language='javascript'>

document.forms[0].submit();

</script>
Run Code Online (Sandbox Code Playgroud)

有没有办法直接通过winforms c#发布数据.我希望能够在发布后访问成功/失败URL并获取重定向站点的查询字符串.

使用参考在这里输入链接描述我已尝试发布但我需要结果查询字符串.

现在我可以通过以下方式实现:

webBrowser1.Url = new Uri("C:\\Documents and Settings\\Admin\\Desktop\\calltie.html");            
webBrowser1.Show();
Run Code Online (Sandbox Code Playgroud)

Jus*_*vey 6

当然,看看WebRequest,这是一个完整的例子

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

然后你可以做这种事情

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
Run Code Online (Sandbox Code Playgroud)

最后一行将为您提供您所追求的URL(成功/失败)


Pey*_*ani 6

是的,您可以使用WebClient类.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

例:

PostMessageToURL("http://tempurl.org","query=param1&query2=param2");
Run Code Online (Sandbox Code Playgroud)