我需要将数据发布到REST服务器。这些数据是动态的,因此我不知道发送该帖子时会有多大,因为我是从其他位置实时获取的。但是,服务器会在收到足够的数据时进行答复。
我尝试使用HttpWebRequest来执行此操作。但是,如果在写入请求流之前调用BeginGetResponse,则写入请求流会导致异常。显然,获得响应使我无法发送其他信息。我该如何判断服务器是否在未获得实际响应的情况下发送了响应?
这是代码。
async private Task SendDataFile(ChannelParameters cp)
{
var uri = new Uri("http://" + Properties.Settings.Default.dnn_servers[0] + ":" + Properties.Settings.Default.dnn_ports[0]);
string request_uri = "http://" + Properties.Settings.Default.dnn_servers[0] + ":" + Properties.Settings.Default.dnn_ports[0] + String.Format("/v1/speech:recognize/{0}?key={1}&confidence-threshold={2}&do-endpointing={3}",
cp.current_name, rest_app_key, Properties.Settings.Default.usConfidenceThreshold, Properties.Settings.Default.endpointing);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(request_uri);
httpWebRequest.Method = "POST";
httpWebRequest.PreAuthenticate = false;
httpWebRequest.AllowWriteStreamBuffering = false;
httpWebRequest.SendChunked = true;
httpWebRequest.Headers["Accept-Language"] = Properties.Settings.Default.Resource.ToString();
httpWebRequest.Accept = "*/*";
//st.Write(b, 0, b.Length);
//st.Write(b, 0, b.Length);
//...
switch (main_window.parameters.stream_format)
{
case 0:
httpWebRequest.ContentType = "audio/L16; rate=8000";
break;
case 1:
httpWebRequest.ContentType = "audio/PCMU; rate=8000";
break;
case 2:
httpWebRequest.ContentType = "audio/PCMA; rate=8000";
break;
}
httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpWebRequest);
//httpWebRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), httpWebRequest); can't remove the comment on this line.
allDone.WaitOne();
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
done_stream = false;
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.GetRequestStream();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
while (stream_data_event.WaitOne() && !done_stream)
{
stream_data_event.Reset();
long write_position = main_window.stream_memory.Position;
main_window.stream_memory.Seek(0, SeekOrigin.Begin);
try
{
postStream.Write(main_window.stream_memory.GetBuffer(), 0, (int)write_position);
}
catch (Exception e)
{
main_window.AddToLog(e.ToString());
}
main_window.stream_memory.Position = 0;
}
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
////done_stream = true;
main_window.StopRecord();
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
allDone.Set();
}
有人尝试过这样的事情吗?