Reputation: 14039
I am writing a simple serverside app that sends request to a remote rest service. When I send it through my code using this code sample:
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
var paramsBuilder = new StringBuilder();
foreach (var param in parameters)
{
paramsBuilder.AppendFormat("&{0}={1}", param.Key, param.Value);
}
paramsBuilder.Length--;
byte[] byteArray = Encoding.UTF8.GetBytes(paramsBuilder.ToString());
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
I am returned with a partial result. The content isn't trimmed, it's just partial. When I send the same request via a service like hurl.it I get the full response.
What could be causing it?
Upvotes: 2
Views: 1234
Reputation: 1322
It may be because of the encoding the server is using in the response
Check this question partial page source from httpwebresponse
which links to http://blogs.msdn.com/b/feroze_daud/archive/2004/03/30/104440.aspx
which explains that "StreamReader uses a default encoding (UTF8) which might not match the encoding of the bytes you are reading into the StreamReader."
Upvotes: 2