Error: This Operation cannot be performed after request has been submitted

前端 未结 2 550
独厮守ぢ
独厮守ぢ 2021-01-23 06:35

I\'ve written code about httpwebrequest and httpwebresponse . I need to send data to server but got this exception \"This operation cannot be performed

2条回答
  •  终归单人心
    2021-01-23 07:20

    when you get the response stream the request is submitted so you can not the operation there...

    Try it like:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://"+remoteServer+":8080/");
    request.Credentials = CredentialCache.DefaultCredentials;
    //Stream NewReqstream = request.GetRequestStream();
    request.Method = "POST";
    //request.ContentLength = cmd.Length;
    
    // Send the command
    //clientSocket.Send(cmd);
    
    Stream NewReqstream = request.GetRequestStream(); //<-- Error here
    NewReqstream.Write(cmd, 0, cmd.Length);
    NewReqstream.Close();
    
    // Get the response
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Console.WriteLine("Connected..");
    

提交回复
热议问题