Try to close the request stream before retrieving the response.
You can accomplish this by using "using" blocks:
public string login(string url, string postData)
{
byte[] requestData = Encoding.UTF8.GetBytes(postData);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = requestData .Length;
using (Stream requestStream = webRequest.GetRequestStream())
{
webpageStream.Write(requestData, 0, requestData.Length);
}
WebResponse webpageResponse = webRequest.GetResponse();
using (responseReader = new StreamReader(webpageResponse.GetResponseStream()))
{
return webpageReader.ReadToEnd();
}
}
(Besides, I've done some style improvements you should adopt, the code will be a far more readable)
For more infos, see
a Tutorial on MSDN.[
^]
And:
Response.Write(responseFromServer);
Never do that!
What is Response? (A rhetorical question)
This is called bad side effects...