我正在尝试使用HttpWebRequest通过POST将数据发送到REST Web服务,然后使用额外的HttpWebRequest跟进,以便在处理后下载响应。
POST正在将二进制数据发送到Web服务,如下所示:
HttpWebRequest uploadRequest = (HttpWebRequest)WebRequest.Create(baseAddress + uploadURIRequest);
uploadRequest.Timeout = Timeout.Infinite;
uploadRequest.ReadWriteTimeout = Timeout.Infinite;
uploadRequest.Method = "POST";
uploadRequest.ContentLength = fileInfo.Length;
uploadRequest.ContentType = "application/octet-stream";
using (Stream writeStream = uploadRequest.GetRequestStream())
{
using (FileStream readStream = new FileStream(fileStreamFileName, FileMode.Open, FileAccess.Read))
{
byte[] data= new byte[readStream.Length];
int bytesRead = readStream.Read(data, 0, (int)readStream.Length);
writeStream.Write(data, 0, bytesRead);
readStream.Close();
}
writeStream.Close();
}
然后,下一个请求是告诉Web服务处理服务器上的数据并返回状态响应。
HttpWebRequest processRequest = (HttpWebRequest)WebRequest.Create(baseAddress + processURIRequest);
processRequest.Timeout = 10000;
processRequest.ReadWriteTimeout = 10000;
processRequest.ContentType = "GET";
HttpWebResponse processRequestResponse = (HttpWebResponse)processRequest.GetResponse();
using (Stream processRequestResponseStream = processRequestResponse.GetResponseStream())
{
//Do stuff...
}
当我更改内容类型时,它适用于XML数据。但是,当我如上所示保留二进制数据时,操作总是超时...即使我将超时增加到超过10秒。它正在进行的处理不应该花这么长时间才能返回。
当我调试时,它总是挂在GetResponse:
的行上HttpWebResponse processRequestResponse = (HttpWebResponse)processRequest.GetResponse();
答案 0 :(得分:1)
添加
HttpWebResponse uploadRequestResponse = (HttpWebResponse)uploadRequest.GetResponse();
写入流后,解决了这个问题。