Reputation: 48
I've googled and searched here. Some suggest that streams were not being close, others suggested that it's a connection limit with ServicePointManager.DefaultConnectionLimit being set to 1. However, none of these seem to work.
My problem is, when i use this for the first time, it works:
using (var stream = request.GetRequestStream())
{
var data = Encoding.UTF8.GetBytes(post.ToString());
stream.Write(data, 0, data.Length);
}
When I use it a second time, it freezes. Yes, I'm disposing my stream, yes im Aborting and closing my response and requests.
Here is my entire code segment:
public string get_hash(string strUsername, string strPassword, string strUniverse)
{
// Request VAR
var request = (HttpWebRequest)WebRequest.Create("http://website.com/");
// Response VAR
var response = (HttpWebResponse)request.GetResponse();
// Cookie Var
var cookie = new CookieContainer();
ServicePointManager.DefaultConnectionLimit = 100;
request.Timeout = 10;
request = (HttpWebRequest)WebRequest.Create("http://website.com/main/login");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
request.Headers.Add("Accept-Language", "en-US,en,q=0.8");
request.Headers.Add("Cache-Control", "max-age=0");
request.ContentType = "application/x-www-form-urlencoded";
request.Host = "website.com";
request.Headers.Add("Origin", "http://website.com");
request.Referer = "http://website.com/";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36";
request.Method = "POST";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookie.GetCookies(request.RequestUri));
// SET POST DATA HERE
var post = HttpUtility.ParseQueryString(string.Empty);
post.Add("uni", strUni);
post.Add("login", strUsername);
post.Add("pass", strPassword);
using (var stream = request.GetRequestStream())
{
var data = Encoding.UTF8.GetBytes(post.ToString());
stream.Write(data, 0, data.Length);
stream.Close();
stream.Dispose();
}
response = (HttpWebResponse)request.GetResponse();
string strSSID = "Failed";
if (response.StatusCode == HttpStatusCode.OK)
{
var data = string.Empty;
using (var sReader = new StreamReader(response.GetResponseStream()))
{
data = sReader.ReadToEnd();
sReader.Close();
sReader.Dispose();
}
string strSSIDurl = response.ResponseUri.ToString();
int intSSIDurlStart = strSSIDurl.IndexOf("PHPSESSID=") + 10;
strSSID = strSSIDurl.Substring(intSSIDurlStart);
}
request.Abort();
response.Close();
response.Dispose();
return strSSID;
}
Upvotes: 1
Views: 1947
Reputation: 91
I know it's an old question. But I spent one hour on this, and found that the answer is the corporation proxy, so adding the following to the web.config solved the issue, just in case someone from business had the same setup as mine and scatched all their hairs off...
<system.net>
<defaultProxy enabled="true"/>
<settings>
</settings>
</system.net>
Upvotes: 0
Reputation: 973
you are not disposing the response from the first request.
var request = (HttpWebRequest)WebRequest.Create("http://website.com/");
var response = (HttpWebResponse)request.GetResponse(); // Not disposed
We had similar problem and disposing the response properly helped us.
Upvotes: 1