Below are a few examples you might want to try... did a little cutting/pasting and didn't try to compile, but should work fine. Wrote these methods a few years ago and have been working fine since.
public class HttpPost
{
static public string GetResponseAsString( string url, int timeout )
{
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create( url );
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 1000 * 30;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
webRequest.PreAuthenticate = true;
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.Proxy = new System.Net.WebProxy("your proxy server", true); // comment out if you're not going thru a proxy
WebResponse webResponse = null;
try
{
webResponse = webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
Encoding encoding = System.Text.Encoding.GetEncoding( "utf-8" );
StreamReader streamReader = new StreamReader( stream, encoding );
string result = streamReader.ReadToEnd();
return result;
}
catch ( Exception e )
{
return e.Message;
}
}
static public HttpWebResponse GetResponse(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
webRequest.Proxy = new System.Net.WebProxy("your proxy server", true); // comment out if you're not going thru a proxy
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 1000 * 30;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
webRequest.PreAuthenticate = true;
webRequest.Credentials = CredentialCache.DefaultCredentials;
return (HttpWebResponse)webRequest.GetResponse();
}
}
http post
Please mark the post as ANSWER if it helps you
Disclaimer: Just my opinion. Not my employer or anyone else....
Rick Matthys
Contributor
2794 Points
406 Posts
Re: HttpWebRequest.GetResponse() throwing 500 error
Jan 21, 2009 05:44 AM|LINK
Below are a few examples you might want to try... did a little cutting/pasting and didn't try to compile, but should work fine. Wrote these methods a few years ago and have been working fine since.
public class HttpPost { static public string GetResponseAsString( string url, int timeout ) { HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create( url ); webRequest.AllowAutoRedirect = true; webRequest.Timeout = 1000 * 30; webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; webRequest.PreAuthenticate = true; webRequest.Credentials = CredentialCache.DefaultCredentials;http post
Disclaimer: Just my opinion. Not my employer or anyone else....