简体   繁体   中英

post more than one data in asp.net with C# and get back the response

I'm trying to post data at "http://www.indianrail.gov.in/cgi_bin/inet_srcdest_cgi_time.cgi" using asp.net with C#. The problem is that the code that i am using is not able to post more than one data. This code has worked properly when i had posted just one data at "http://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgi" however this is not working when i am trying to post more than one data. Also there is one hidden field attached with each input data, so what do i have to do with that hidden field.

    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();

In a POST you simply provide a stream of data. Is that stream is to contain "two" things, you simply serialize both to the stream. In your case, if you want to send more thane one query string parameter, simply add that on to the string before you call GetBytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM