[英]How to Post Data to a website through win forms c#
我有一個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#發布數據。 我希望能夠在發布后訪問成功/失敗URL並獲取重定向站點的查詢字符串。
使用參考在這里輸入鏈接描述我已嘗試發布但我需要結果查詢字符串。
現在我可以通過以下方式實現:
webBrowser1.Url = new Uri("C:\\Documents and Settings\\Admin\\Desktop\\calltie.html");
webBrowser1.Show();
當然,看看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(成功/失敗)
是的,您可以使用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");
我目前正在研究它..這是運行代碼試試吧..
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”)更改為您要發送的鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.