Reputation: 634
Using form contenttype, I am getting stuck at this line:
var webresponse = (HttpWebResponse)webrequest.GetResponse();
For the url, you can use any value. Here is the full code:
public bool test()
{
string url = "https://www.google.com";
// Create a new HTTP request object, set the method to POST and write the POST data to it
var webrequest = (HttpWebRequest)WebRequest.CreateHttp(url);
webrequest.Method = "POST";
webrequest.ContentType = "application/x-www-form-urlencoded";
using (Stream postStream = webrequest.GetRequestStream())
{
//
}
// Make the request, get a response and pull the data out of the response stream
var webresponse = (HttpWebResponse)webrequest.GetResponse();
Stream responseStream = webresponse.GetResponseStream();
var reader = new StreamReader(responseStream);
string result = reader.ReadToEnd();
// Normal Completion
return true;
}
Does anyone know why it would get stuck on this line and not come back (when you are doing debugging):
var webresponse = (HttpWebResponse)webrequest.GetResponse();
Thank You
Upvotes: 1
Views: 207
Reputation: 634
I found the problem! I was calling the method as an async task:
private async Task starttest()
{
clsTester objTester = new clsTester();
objTester.test();
}
...
var result = Task.Run(async () => await starttest());
I removed all that code and changed it to a worker thread:
private Thread workerThread = null;
private void starttest()
{
clsTester objTester = new clsTester();
objTester.test();
}
And then called it as follows:
...
// Initialise and start worker thread
this.workerThread = new Thread(new ThreadStart(this.starttest));
this.workerThread.Start();
Everything is working now.
Upvotes: 1