I’m trying to perform a programmatic HTTP POST in an ASP.NET MVC 3 application. The aim is to mimic a form submission.
I’m using the following code (modified slightly for confidentiality):
string input = "foo=bar";
byte[] data = Encoding.UTF8.GetBytes(input);
var request = (HttpWebRequest)WebRequest.Create("http://my.domain.com/endpoint");
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
using ( var stream = request.GetRequestStream() ) {
stream.Write(request);
stream.Close();
}
var response = request.GetResponse();
// get the response data, do backflips, save the world
The problem is, I’m getting an error during the .GetRequestStream() call, saying that I couldn’t connect to the URL resource.
However, if I plug in the exact same code in a console application, I can connect just fine. A colleague also managed to connect using the same code in a WCF service application.
This is driving me nuts. Any help on this would be much appreciated.
This sounds like a security issue to me.
To fix try the following:
If this doesnt work then can you please add more info e.g. Fiddler logs, stack trace.