我想發佈到WCF服務。我收到此錯誤 「在調用[Begin] GetResponse之前,您必須將ContentLength字節寫入請求流。」REST POST到WCF服務
代碼:
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "patron=WTM";
byte[] data = encoding.GetBytes(postData);
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8733/FileShareWebServices/UploadFile");
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = data.Length;
httpWebRequest.GetRequestStream();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
我想你需要經過移動的GetResponse關閉流(在StreamReader的using語句之後移動它) – Sparrow
@Sparrow - 這將不起作用,很可能會導致OP代碼無法編譯。對'GetResponse'的調用用於在'using'塊中創建'StreamReader'。 – Tim