ContentLength not set after write - c#

I'm trying to send data to a server with this function :
public static async Task<Byte[]> sendData(Byte[] bt)
{
try
{
System.Net.HttpWebRequest myWebRequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
myWebRequest.Method = "POST";
myWebRequest.ContentType = "text/xml";
myWebRequest.AllowReadStreamBuffering = true;
using (var requestStream = (Stream)(await Task<Stream>.Factory.FromAsync(myWebRequest.BeginGetRequestStream, myWebRequest.EndGetRequestStream, myWebRequest)))
{
requestStream.Write(bt, 0, bt.Length);
}
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(myWebRequest.BeginGetResponse, myWebRequest.EndGetResponse, myWebRequest)))
{
using (var responseStream = response.GetResponseStream())
{
bt = new byte[responseStream.Length];
responseStream.Read(bt, 0, bt.Length);
return bt;
}
}
}
catch (Exception ex)
{
return null;
}
}
I get a response from the server saying that no data was sent (ContentLength == 0). Indeed, if I look at myWebRequest.ContentLength after writing in the stream request, _ContentLength is set to 0 and ContentLengthis set to -1.
Did I miss something ?
Thanks for your help !

Related

How to properly post data using begin/end GetRequestStream and callbacks

I'm trying to post a piece of data but it seems to be getting caught and hangs (but not throwing an error).
internal IAsyncResult RequestGet<TPostType>(Action<string> callback, string path, TPostType value)
{
var http = (HttpWebRequest)WebRequest.Create(Connection.GetUri(path).Uri);
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
var parsedContent = JsonConvert.SerializeObject(value);
var encoding = new ASCIIEncoding();
var bytes = encoding.GetBytes(parsedContent);
return http.BeginGetRequestStream(ar =>
{
try
{
var stream = http.EndGetRequestStream(ar);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
http.BeginGetResponse(body =>
{
// its around here that it hangs.
var response = http.EndGetResponse(ar);
var s = response.GetResponseStream();
ReadStream(callback, s);
}, null);
}
catch (WebException webex)
{
WebResponse errResp = webex.Response;
using (Stream respStream = errResp.GetResponseStream())
{
Console.WriteLine(new StreamReader(respStream).ReadToEnd());
}
}
}, null);
}
There were a few times when it didn't hang but told me that the request body was missing, however I didn't make any progress beyond that. Could someone please inform me as to what I'm doing wrong?
The ReadStream method works for GET requests, but for context:
private static IAsyncResult ReadStream(Action<string> callback, Stream stream)
{
var buffer = new byte[5000];
var asyncResult = stream.BeginRead(buffer, 0, 5000, ar2 =>
{
try
{
callback(Encoding.UTF8.GetString(buffer, 0, 5000));
}
catch (Exception ex)
{
// do better error handling
Console.WriteLine("exception reading stream");
Console.WriteLine(ex);
}
}, null);
return asyncResult;
}
For more context on how its being used:
public IAsyncResult BeginGetChainPart(Action<BlockData.BlockList> callback, int height)
{
var asyncResult = new HttpConnector(Connection).RequestGet(body =>
{
callback(JsonConvert.DeserializeObject<BlockData.BlockList>(body));
}, "/local/chain/blocks-after", new BlockData.BlockHeight { Height = height });
return asyncResult;
}
found the solution, posting in case anyone comes across the same issue:
internal IAsyncResult RequestGet<TPostType>(Action<string> callback, string path, TPostType value)
{
var http = (HttpWebRequest)WebRequest.Create(Connection.GetUri(path).Uri);
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
var parsedContent = JsonConvert.SerializeObject(value);
var encoding = new ASCIIEncoding();
var bytes = encoding.GetBytes(parsedContent);
return http.BeginGetRequestStream(ar => {
try
{
var stream = http.EndGetRequestStream(ar);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
var response = http.GetResponse();
var responseStream = response.GetResponseStream();
ReadStream(callback, responseStream);
}
catch (WebException webex)
{
WebResponse errResp = webex.Response;
using (Stream respStream = errResp.GetResponseStream())
{
StreamReader reader = new StreamReader(respStream);
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}, null);
}

Can't get ResponseStream from WebException

I have a desktop client, that communicates with serverside via Http.
When server has some issues with data processing it returns description of an error in JSON in Http response body with proper Http-code (mainly it is HTTP-400).
When i read HTTP-200 response everithing's fine and this code works:
using (var response = await httpRequest.GetResponseAsync(token))
{
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
{
return await reader.ReadToEndAsync();
}
}
But when an error occures and WebException is thrown and caught there is this code:
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
using (var response = (HttpWebResponse) ex.Response)
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
{
var json = reader.ReadToEnd();
}
}
}
}
}
I have already done something to it to maybe make it work, but the next happens:
response.ContentLength is valid (184)
but stream.Length is 0
and after that i can't read json (it's "")
I don't even know where to look, because everything looks like it should work.
What might be the problem?
After a month of almost everyday thinking I've found workaround.
The thing was that WebException.Response.GetResponseStream() returns not exactly the same stream that was obtained during request (can't find link to msdn right now) and by the time we get to catch the exception and read this stream the actual response stream is lost (or something like that, don't really know and was unable to find any info on the net, except looking into CLRCore which is now opensource).
To save the actual response until catching WebException you must set KeepAlive Property on your HttpRequest and voila, you get your response while catching exception.
So the working code looks like that:
try
{
var httpRequest = WebRequest.CreateHttp(Protocol + ServerUrl + ":" + ServerPort + ServerAppName + url);
if (HttpWebRequest.DefaultMaximumErrorResponseLength < int.MaxValue)
HttpWebRequest.DefaultMaximumErrorResponseLength = int.MaxValue;
httpRequest.ContentType = "application/json";
httpRequest.Method = method;
var encoding = Encoding.GetEncoding("utf-8");
if (httpRequest.ServicePoint != null)
{
httpRequest.ServicePoint.ConnectionLeaseTimeout = 5000;
httpRequest.ServicePoint.MaxIdleTime = 5000;
}
//----HERE--
httpRequest.KeepAlive = true;
//----------
using (var response = await httpRequest.GetResponseAsync(token))
{
using (var reader = new StreamReader(response.GetResponseStream(), encoding))
{
return await reader.ReadToEndAsync();
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
using (var response = (HttpWebResponse)ex.Response)
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
{
return reader.ReadToEnd();
//or handle it like you want
}
}
}
}
}
I don't know if it is good to keep all connection alive like that, but since it helped me to read actual responses from server, i think it might help someone, who faced the same problem.
EDIT: Also it is important not to mess with HttpWebRequest.DefaultMaximumErrorResponseLength.
I remember facing similar issue before and there was something related to setting the stream's position. Here is one of my solutions for reading webResponse that worked for me earlier. Please try if similar approach works for you:-
private ResourceResponse readWebResponse(HttpWebRequest webreq)
{
HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
HttpWebResponse webresp = null;// = webreq.GetResponse() as HttpWebResponse;
var memStream = new MemoryStream();
Stream webStream;
try
{
webresp = (HttpWebResponse)webreq.GetResponse();
webStream = webresp.GetResponseStream();
byte[] readBuffer = new byte[4096];
int bytesRead;
while ((bytesRead = webStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
memStream.Write(readBuffer, 0, bytesRead);
}
catch (WebException e)
{
var r = e.Response as HttpWebResponse;
webStream = r.GetResponseStream();
memStream = Read(webStream);
var wrongLength = memStream.Length;
}
memStream.Position = 0;
StreamReader sr = new StreamReader(memStream);
string webStreamContent = sr.ReadToEnd();
byte[] responseBuffer = Encoding.UTF8.GetBytes(webStreamContent);
//......
//.......
Hope this helps!

The request was aborted: The connection was closed unexpectedly

I am using Jon Skeet's ReadFully method implemented here:
public static byte[] ReadFully(Stream stream)
{
var buffer = new byte[32768];
using (var ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
It throws an exception at the line:
int read = stream.Read(buffer, 0, buffer.Length);
The error message is The request was aborted: The connection was closed unexpectedly.
I am sending an xml request to a webservice. My send method looks like this:
private static string SendRequest(XElement request, string url)
{
var req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "application/soap+xml;";
req.Method = "POST";
req.KeepAlive = false;
req.Timeout = System.Threading.Timeout.Infinite;
req.ReadWriteTimeout = System.Threading.Timeout.Infinite;
req.ProtocolVersion = HttpVersion.Version10;
req.AllowWriteStreamBuffering = false;
using (var stm = req.GetRequestStream())
{
using (var stmw = new StreamWriter(stm))
{
stmw.Write(request.ToString());
}
}
Stream responseStream;
using (var webResponse = req.GetResponse())
{
responseStream = webResponse.GetResponseStream();
}
// Do whatever you need with the response
var myData = ReadFully(responseStream);
string responseString = Encoding.ASCII.GetString(myData);
return responseString;
}
I tried without and without the following variables set and it gives me the same message:
req.KeepAlive = false;
req.Timeout = System.Threading.Timeout.Infinite;
req.ReadWriteTimeout = System.Threading.Timeout.Infinite;
req.ProtocolVersion = HttpVersion.Version10;
req.AllowWriteStreamBuffering = false;
The problem is in this part of your code:
// wrong way to do it!
Stream responseStream;
using (var webResponse = req.GetResponse())
{
responseStream = webResponse.GetResponseStream();
}
// Do whatever you need with the response
var myData = ReadFully(responseStream);
You're disposing your response object before reading from its stream. Try something like this instead:
byte[] myData;
using (var webResponse = req.GetResponse())
{
var responseStream = webResponse.GetResponseStream();
myData = ReadFully(responseStream); // done with the stream now, dispose of it
}
// Do whatever you need with the response
string responseString = Encoding.ASCII.GetString(myData);

Code analysis complains that object can be disposed more than once. Why?

I get warning on responseStream in following function:
private static string GetResponseString(WebResponse response)
{
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (var responseReader = new StreamReader(responseStream))
{
var strResponse = responseReader.ReadToEnd();
return strResponse;
}
}
}
return string.Empty;
}
I call this function from places like like this one:
var request = (HttpWebRequest)WebRequest.Create(Uri);
request.Headers.Add("Authorization", "GoogleLogin auth=" + this.SecurityToken);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = 5000;
// build the post string
var postString = new StringBuilder();
postString.AppendFormat("registration_id={0}", recipientId);
postString.AppendFormat("&data.payload={0}", message);
postString.AppendFormat("&collapse_key={0}", collapseKey);
// write the post-string as a byte array
var requestData = Encoding.ASCII.GetBytes(postString.ToString());
request.ContentLength = requestData.Length;
var requestStream = request.GetRequestStream();
requestStream.Write(requestData, 0, requestData.Length);
requestStream.Close();
// Do the actual request and read the response stream
try
{
var response = request.GetResponse();
var responseString = GetResponseString(response);
response.Close();
return responseString.Contains("id=")
? SendStatus.Ok
: GetSendStatusFromResponse(responseString);
}
catch (WebException ex)
{
var webResponse = (HttpWebResponse)ex.Response;
if (webResponse != null)
{
if (webResponse.StatusCode.Equals(HttpStatusCode.Unauthorized))
{
return SendStatus.Unauthorized;
}
if (webResponse.StatusCode.Equals(HttpStatusCode.ServiceUnavailable))
{
return SendStatus.ServiceUnavailable;
}
}
this.LoggerService.Log(null, ex);
return SendStatus.GeneralException;
}
StreamReader takes ownership of the stream passed to it in the constructor call in the sense that it will call Dispose on it when the StreamReader itself is closed - hence it will already be disposed when the outer Using statement attempts to dispose of it.

How to make a HTTP PUT request?

What is the best way to compose a rest PUT request in C#?
The request has to also send an object not present in the URI.
using(var client = new System.Net.WebClient()) {
client.UploadData(address,"PUT",data);
}
My Final Approach:
public void PutObject(string postUrl, object payload)
{
var request = (HttpWebRequest)WebRequest.Create(postUrl);
request.Method = "PUT";
request.ContentType = "application/xml";
if (payload !=null)
{
request.ContentLength = Size(payload);
Stream dataStream = request.GetRequestStream();
Serialize(dataStream,payload);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string returnString = response.StatusCode.ToString();
}
public void Serialize(Stream output, object input)
{
var ser = new DataContractSerializer(input.GetType());
ser.WriteObject(output, input);
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
var values = string.Format("Name={0}&Family={1}&Id={2}", NameToUpdateTextBox.Text, FamilyToUpdateTextBox.Text, IdToUpdateTextBox.Text);
var bytes = Encoding.ASCII.GetBytes(values);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("http://localhost:51436/api/employees"));
request.Method = "PUT";
request.ContentType = "application/x-www-form-urlencoded";
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
var response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
UpdateResponseLabel.Text = "Update completed";
else
UpdateResponseLabel.Text = "Error in update";
}
How to use PUT method using WebRequest.
//JsonResultModel class
public class JsonResultModel
{
public string ErrorMessage { get; set; }
public bool IsSuccess { get; set; }
public string Results { get; set; }
}
// HTTP_PUT Function
public static JsonResultModel HTTP_PUT(string Url, string Data)
{
JsonResultModel model = new JsonResultModel();
string Out = String.Empty;
string Error = String.Empty;
System.Net.WebRequest req = System.Net.WebRequest.Create(Url);
try
{
req.Method = "PUT";
req.Timeout = 100000;
req.ContentType = "application/json";
byte[] sentData = Encoding.UTF8.GetBytes(Data);
req.ContentLength = sentData.Length;
using (System.IO.Stream sendStream = req.GetRequestStream())
{
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();
}
System.Net.WebResponse res = req.GetResponse();
System.IO.Stream ReceiveStream = res.GetResponseStream();
using (System.IO.StreamReader sr = new
System.IO.StreamReader(ReceiveStream, Encoding.UTF8))
{
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
Out += str;
count = sr.Read(read, 0, 256);
}
}
}
catch (ArgumentException ex)
{
Error = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}", ex.Message);
}
catch (WebException ex)
{
Error = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
}
catch (Exception ex)
{
Error = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
}
model.Results = Out;
model.ErrorMessage = Error;
if (!string.IsNullOrWhiteSpace(Out))
{
model.IsSuccess = true;
}
return model;
}

Categories