Reputation: 1
I have this application coded in c#. The application uses httpclient and webrequests methods.
The problem I am facing is when I run the application it do not actually send requests. But, when I run the fiddler then run the application it sends requests and works flawlessly. What could be the reason?
This sends request.
public void Login()
{
Loginfirst();
//Create cookies
// create request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www." + server_site + ".com/login");
//request.CookieContainer = cookieJar;
request.Method = "POST";
string postData = "email=" + Username + "&password=" + Password + "&remember=false&request_type=ajax";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//set the contenttype property
request.ContentType = "application/x-www-form-urlencoded";
request.ServicePoint.Expect100Continue = true;
//request.AllowAutoRedirect = true;
//set header
request.UserAgent = User_Agent;
request.Accept = "application/json";
request.Headers.Add("X-Requested-with", "XMLHttpRequest");
request.Headers.Add("Cookie", m_cookies);
log_file("line 975" + m_cookies);
//request.Headers["Cookie"] = "rlogin=" + Username + "";
request.KeepAlive = false;
//set the contentlength property of the WebRequest
request.ContentLength = byteArray.Length;
// get the stream containing returned by the server.
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
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookieCount = cookieJar.Count;
String setcookieheader = response.Headers[HttpResponseHeader.SetCookie];
Console.WriteLine(setcookieheader);
foreach (Cookie cookie in response.Cookies)
{
if (cookie.Name == "PHPSESSID")
{
PHPSESSID = cookie.Value;
logged = true;
grid_status = "Log OK";
log_file("Grid Status OK");
}
}
//display statuts
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Status OK");
log_file("Status OK 1022");
all_other_requests("profile");
all_other_requests("profile_status");
all_other_requests("faster");
//all_other_requests("fasterstart");
real_timer.Elapsed += Real_timer_Elapsed;
real_timer.Interval = 1000;
real_timer.Enabled = true;
real_timer.Start();
}
else
{
Login();
}
dataStream = response.GetResponseStream();
//open the stream using a streamreader for easy access.
StreamReader reader = new StreamReader(dataStream);
//read the content
//response_object = reader.ReadToEnd().Replace("\\/", "/").Replace("\"", "\'");
response_object = reader.ReadToEnd();
Console.WriteLine(response_object);
reader.Close();
response.Close();
}
catch (WebException e)
{
log_file("Line 1055" + e.Message);
if (e.Status == WebExceptionStatus.Timeout)
{
Login();
return;
}
}
//dynamic result = JsonConvert.DeserializeObject(response_object);
/*
if (result.response.result=="bad_auth")
{
logged = false;
}
if(result.response.result == "ok_auth")
{
//MessageBox.Show("logged in");
Session = result.response.session;
logged = true;
all_other_requests("get");
} */
}
t0 = new Task(bb0.Login);
t0.Start();
t1 = new Task(bb1.Login);
t1.Start();
t2 = new Task(bb2.Login);
t2.Start();
Task t0 works without fiddler. Task t1 wont work without fiddler. Task t2 wont work without fiddler.
You can only successfully one task at a time. Like if you run t2 then cant work with t1, t2 vice verse.
Upvotes: 0
Views: 118
Reputation: 578
This only creates the request object. You have to call GetResponse()
or BeginGetResponse()
to actually send the request to the server.
WebResponse reponse = request.GetResponse();
Upvotes: 1