0
我想從內部使用C#控制檯應用程序發佈此表單中的數據。但我不明白爲什麼它不工作。以下是html頁面的body標籤內的表單。在C#控制檯應用程序中將表單數據發佈到服務器
<form method="POST" name="frogs_simple_vmf" action="frog_simple.php">
<table BORDER="0" cellpadding="5" cellspacing="0" border="0" class="tbl">
<tr>
<td>
<select name='from' onchange='check_ns2(this.form)'>
<option value='Sub1'>Sub1</option>
<option value='Sub2'>Sub2</option>
<option value='Sub3'>Sub3</option>
<option value='Sub4'>Sub4</option>
</select>
 
<input type='text' name='spec_id' size='30'>
</td>
</tr>
<tr>
<td>
<table WIDTH="400" BORDER="0" cellpadding="0" cellspacing="0" >
<tr>
<td WIDTH="150"><b><a href="javascript:popup('../help/help_avp_light_eu.htm#Run_option', '500', '300');" class="lbl">Run Option:</a></td>
<td><b><a href="javascript:popup('../help/help_avp_light_eu.htm#DCN_inter', '500', '300');" class="lbl">DCN Interpretation:</a></td>
</tr>
</table>
</td>
<td><b><a href="javascript:popup('../help/help_avp_light_eu.htm#User_name', '500', '300');" class="lbl">User Name:</a></td>
</tr>
<tr><td><select name='output' onchange='check_ns2(this.form)'>
<option value='Va1'>Va2</option>
<option value='Va2'>Va2</option>
</select>  <input type='text' name='build' size=6> (YYYYWW)</td>
<td><input type='text' id="time1" name='username' size=12></td>
</tr>
<tr>
<td colspan="2" class="buttonRow">
<input TYPE="HIDDEN" name="com_site" value="comvmf">
<input TYPE="SUBMIT" name="Execute" VALUE="Submit" class="btn">
<input TYPE="RESET" VALUE="Reset" class="btn">
</td>
</tr>
</table>
</form>
這是我的代碼:
public static string HttpPostWithCookie(string uri, string parameters, CookieContainer cookieContainer, out string result)
{
result = "ok";
// parameters: name1=value1&name2=value2
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.CookieContainer = cookieContainer;
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(parameters);
System.IO.Stream os = null;
try
{ // send the Post
webRequest.ContentLength = bytes.Length; //Count bytes to send
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Send it
}
catch (WebException ex)
{
result = "HttpPost: Request error" + ex.Message;
}
finally
{
if (os != null)
{
os.Close();
}
}
try
{ // get the response
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
{
result = "Empty response";
return null;
}
System.IO.StreamReader sr = new System.IO.StreamReader(webResponse.GetResponseStream());
return sr.ReadToEnd().Trim();
}
catch (WebException ex)
{
result = "HttpPost: Response error" + ex.Message;
}
return null;
} // end HttpPost
不幸的是我不能發佈的數據。有沒有其他的方法可以將數據發佈到網頁「frog_simple.php」?我所有人都收到了一個web反應,說「>>錯誤。沒有數據提交」。
您可以使用Fiddler準確查看代碼發送的請求,並將其與您在提交表單時瀏覽器發送的內容進行比較。 –