我编写了关于httpwebrequest
和httpwebresponse
的代码。我需要将数据发送到服务器但得到此异常“在提交请求后无法执行此操作”。请参阅以下代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://"+remoteServer+":8080/");
request.Credentials = CredentialCache.DefaultCredentials;
//Stream NewReqstream = request.GetRequestStream();
request.Method = "POST";
//request.ContentLength = cmd.Length;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Connected..");
// Send the command
//clientSocket.Send(cmd);
Stream NewReqstream = request.GetRequestStream(); //<-- Error here
NewReqstream.Write(cmd, 0, cmd.Length);
NewReqstream.Close();
答案 0 :(得分:3)
当您收到响应流时,请求已提交,因此您无法在那里进行操作......
尝试如下:
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..");
答案 1 :(得分:1)
例外情况告诉你你需要知道什么; HTTP是request =&gt;响应,所以如果您要求回复(GetResponse()
),您已经隐含地说“我的请求已完成;请给我回复”。移动GetRequestStream()
代码 {/ 1}}。