0

データを Web サイトに投稿し、サーバーから応答を取得しようとしています。これは私が使用しているコードです:

    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create("http://www.indianrail.gov.in/train_Schedule.html");
    ((HttpWebRequest)request).UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:9.0) Gecko/20100101 Firefox/9.0";
    // Set the Method property of the request to POST.
    request.Method = "POST";
    // Create POST data and convert it to a byte array.
    string postData = lccp_trnname.Text;
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream();
    // Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length);
    // Close the Stream object.
    dataStream.Close();
    // Get the response.
    try
    {
        WebResponse response = request.GetResponse();
        // Display the status.
        Response.Write(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Response.Write(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
    }
    catch (WebException ee)
    {
        Label1.Text = ee.Message;

    }

サーバーから返信を受け取る代わりに、データを投稿しているのと同じ Web ページにリダイレクトされます。私のコードで何が問題になったのか、誰かが何か考えを持っている場合は、助けてください。私はずっと前から試みてきましたが、すべての努力は無駄でした. だから助けてください

4

1 に答える 1

2

http://www.indianrail.gov.in/train_Schedule.htmlではなくhttp://www.indianrail.gov.in/cgi_bin/inet_trnnum_cgi.cgiにデータを投稿する必要があります

アップデート:

2番目の問題は、データで「lccp_trnname」パラメータの名前を送信していないことです。これで動作します:

string postData = "lccp_trnname=" + lccp_trnname.Text;
于 2012-04-18T19:54:20.657 に答える