我有一个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"> </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#发布数据。 我希望能够在发布后访问成功/失败网址并获取重定向网站的查询字符串。
参考enter link description here我试过发帖,但我需要结果查询字符串。
现在我可以通过以下方式实现这一目标:
webBrowser1.Url = new Uri("C:\\Documents and Settings\\Admin\\Desktop\\calltie.html");
webBrowser1.Show();
答案 0 :(得分: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(成功/失败)
答案 1 :(得分: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;
}
}
示例:
PostMessageToURL("http://tempurl.org","query=param1&query2=param2");
答案 2 :(得分: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中的网址(“www.linktoposton.php”)更改为您要发送的链接