7

私はいくつかのデータを送信して投稿し、投稿されたデータが有効かどうかを取得できるAPIを持っています。この 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 に答える