7

我有一个 api,我可以在其中发布一些数据 n 提交,然后获取发布的数据是否有效。此 api 重定向到指示成功/失败的不同 url。为此,我通常做的是,在 html 标记中调用目标 url 并提交页面:

    <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>

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

参考在此处输入链接描述我尝试过发布,但我需要结果查询字符串。

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

webBrowser1.Url = new Uri("C:\\Documents and Settings\\Admin\\Desktop\\calltie.html");            
webBrowser1.Show();
4

3 回答 3

7

是的,您可以使用 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;
    }
}

例子:

PostMessageToURL("http://tempurl.org","query=param1&query2=param2");
于 2012-12-17T11:30:59.243 回答
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

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

于 2012-12-17T11:26:39.803 回答
0

我目前正在研究它..这是运行代码尝试它..

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("www.linktoposton.php");
        req.Method = "POST";
        byte[] byteArray = Encoding.UTF8.GetBytes(content);
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = byteArray.Length;
        Stream dataStream = req.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        WebResponse response = req.GetResponse();
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = HttpUtility.UrlDecode(reader.ReadToEnd());
        reader.Close();
        dataStream.Close();
        response.Close();
        Application.DoEvents();   // optional

只需更改 Httpwebrequest 中的 url(“www.linktoposton.php”为您要发送的链接)

于 2012-12-19T09:17:28.920 回答