我试图在" http://www.indianrail.gov.in/cgi_bin/inet_srcdest_cgi_time.cgi"使用asp.net与C#。问题是我使用的代码无法发布多个数据。当我在" http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi"上发布了一个数据时,此代码已正常运行。但是,当我尝试发布多个数据时,这不起作用。每个输入数据都附有一个隐藏字段,因此我必须对该隐藏字段做什么。
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "lccp_src_stncode=" + src;
postData += ("&lccp_dstn_stncode=" + des);
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.indianrail.gov.in/cgi_bin/inet_srcdest_cgi_time.cgi");
myRequest.Method = "POST";
myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
//Get the stream containing content returned by the server.
newStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(newStream,Encoding.UTF8);
// Read the content.
string responseFromServer = reader.ReadToEnd();
File.WriteAllText(@"C:\Users\Zohaib\Documents\Visual Studio 2010\WebSites\WebSite4\App_Data\data.txt", responseFromServer);
// Clean up the streams.
reader.Close();
newStream.Close();
response.Close();
答案 0 :(得分:0)
在POST中,您只需提供一个数据流。该流是否包含“两个”内容,您只需将它们序列化为流。在您的情况下,如果您想发送更多一个查询字符串参数,只需在调用GetBytes之前将其添加到字符串