3
私は複数の投稿要求をウェブサイトに送信したいと思います。 私はGoogleの上で検索し、このコードを持っている:複数の投稿依頼を送信
class Program
{
public static HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://example.com/login.php");
public static ASCIIEncoding encoding = new ASCIIEncoding();
public static string postData = "";
public static bool first = true;
static void Main(string[] args)
{
string uname = "", pass = "", Final = "";
while (1>0)
{
Console.WriteLine("Enter uname ,then password");
uname = Console.ReadLine();
pass = Console.ReadLine();
if (uname == "0")
break;
Final = letstry(uname, pass);
Console.WriteLine(Final);
Console.WriteLine("Finish That");
}
}
public static string letstry(string uname, string pass)
{
postData = "uname=" + uname;
postData += ("&pass=" + pass);
byte[] data = encoding.GetBytes(postData);
if (first)
{
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.ContentLength = data.Length;
first = !first;
}
Stream newStream = HttpWReq.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
WebResponse resp = HttpWReq.GetResponse();
StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
}
私は接続が閉じられましたというエラー(newstreamに)を取得しています。 複数のリクエストを送信するのに同じ接続を使用できないのはなぜですか?
を作成する代わりに、letstry
にストリームvarを送信することしか考えられません。
私は専門家ではありませんので、不必要な間違いがありましたら申し訳ありません。 1つの要求を行う:あなたの助けのための
tyvm :)
なぜwhile(1> 0)と書いていますか? 'while(true)'ははるかに明確です。 – svick
@スヴィク:「たくさん」? – Tim
@ティム、そうですね。私はその行で一時停止し、それが実際に意味するものを考えなければなりませんでした。 – svick