G
Glass Half Full
I'm trying to get the WebRequest object working with a using post. I imagine I doing something simple and obvious wrong but I just don't see it.
I've included the code that is at the heart of what I'm doing. When I run this using "get" method ("m_HTMLmethod = "get"), everything works mostly as expected. I say mostly because I don't understand why it sends two unauthorized requests. In Fiddler what I see is the first unauth request (expected), a 401 returned, I'm prompted for a userid/password, but then I see another unauth request and another 401 returned, then it sends the authorizied request (header: "Authorization: Basic aWRi..."), even though I have PreAuthenticate set to true (perhaps that doesn't do what I think it does). In any case with the "get" I'm able to eventually read the expected HTML response.
When I try this code using "post", I'm prompted for credentials and I see (in Fiddler) the two unauthorized requests, then I get back the following (WebException):
"The underlying connection was closed: An unexpected error occurred on a send."
Is there a trick (or do I have an bug) in getting WebRequest to work as a post?
Thanks
//---- Code fragment -----
StringBuilder sb = new StringBuilder();
foreach (string name in m_parms.Keys) {
ArrayList al = (ArrayList) m_parms[name];
for (int i = 0; i < al.Count; i++) {
string v = (string) al;
if (v == null) { v = ""; }
sb.Append(name + "=" + System.Web.HttpUtility.UrlEncode(v) + "&");
}
}
string args = sb.ToString();
WebResponse wrp = null;
try {
GetCreds gc = new GetCreds();
wrp = ReadUri(args, gc.Domain, gc.User, gc.Password);
}
catch (WebException e0) {
string msg0 = e0.Message;
if (msg0.IndexOf("401") < 0) {
throw e0;
}
GetCreds gc = new GetCreds();
wrp = e0.Response;
string auth = wrp.Headers.Get("WWW-Authenticate");
string cookie = wrp.Headers.Get("Set-Cookie");
while (true) {
DialogResult dr = gc.ShowDialog();
if (dr == DialogResult.OK) {
try {
wrp = ReadUri(args, gc.Domain, gc.User, gc.Password);
break; // break out of while loop
}
catch (WebException e1) {
string msg1 = e1.Message;
if (msg1.IndexOf("401") < 0) {
throw e1;
}
}
}
else {
return ix;
}
}
}
StreamReader srdr = new StreamReader(wrp.GetResponseStream());
m_browserOutput.SetContent(srdr.ReadToEnd());
//____________________________________________________________________________________________
// this method uses m_HTMLaction and m_HTMLmethod globals and returns WebResponse object
private WebResponse ReadUri(string args, string domain, string user, string password) {
WebRequest wrq = null;
WebResponse wrp = null;
string uri = m_HTMLaction;
// if it's a get append args to uri string
if (m_HTMLmethod == "get") {
uri += "?" + args;
}
//--------------------------------------------------------------------------------
// hook Fiddler up
Uri proxyURI = new Uri("http://127.0.0.1:8888");
GlobalProxySelection.Select = new WebProxy(proxyURI);
//--------------------------------------------------------------------------------
wrq = WebRequest.Create(uri);
wrq.Method = m_HTMLmethod;
wrq.ContentType = "application/x-www-form-urlencoded";
string proxy = System.Configuration.ConfigurationSettings.AppSettings["HTML_ProxyURL"];
if ((proxy != null) && (proxy != "")) {
wrq.Proxy = new WebProxy(proxy);
}
if (user != "") {
if (domain == "") {
wrq.Credentials = new NetworkCredential(user, password);
}
else {
wrq.Credentials = new NetworkCredential(user, password, domain);
}
wrq.PreAuthenticate = true;
}
// if it's a post write args to stream
if (m_HTMLmethod == "post") {
wrq.ContentLength = args.Length + 2;
StreamWriter sw = new StreamWriter(wrq.GetRequestStream());
sw.WriteLine(args);
sw.Flush();
}
wrp = wrq.GetResponse();
return wrp;
}
I've included the code that is at the heart of what I'm doing. When I run this using "get" method ("m_HTMLmethod = "get"), everything works mostly as expected. I say mostly because I don't understand why it sends two unauthorized requests. In Fiddler what I see is the first unauth request (expected), a 401 returned, I'm prompted for a userid/password, but then I see another unauth request and another 401 returned, then it sends the authorizied request (header: "Authorization: Basic aWRi..."), even though I have PreAuthenticate set to true (perhaps that doesn't do what I think it does). In any case with the "get" I'm able to eventually read the expected HTML response.
When I try this code using "post", I'm prompted for credentials and I see (in Fiddler) the two unauthorized requests, then I get back the following (WebException):
"The underlying connection was closed: An unexpected error occurred on a send."
Is there a trick (or do I have an bug) in getting WebRequest to work as a post?
Thanks
//---- Code fragment -----
StringBuilder sb = new StringBuilder();
foreach (string name in m_parms.Keys) {
ArrayList al = (ArrayList) m_parms[name];
for (int i = 0; i < al.Count; i++) {
string v = (string) al;
if (v == null) { v = ""; }
sb.Append(name + "=" + System.Web.HttpUtility.UrlEncode(v) + "&");
}
}
string args = sb.ToString();
WebResponse wrp = null;
try {
GetCreds gc = new GetCreds();
wrp = ReadUri(args, gc.Domain, gc.User, gc.Password);
}
catch (WebException e0) {
string msg0 = e0.Message;
if (msg0.IndexOf("401") < 0) {
throw e0;
}
GetCreds gc = new GetCreds();
wrp = e0.Response;
string auth = wrp.Headers.Get("WWW-Authenticate");
string cookie = wrp.Headers.Get("Set-Cookie");
while (true) {
DialogResult dr = gc.ShowDialog();
if (dr == DialogResult.OK) {
try {
wrp = ReadUri(args, gc.Domain, gc.User, gc.Password);
break; // break out of while loop
}
catch (WebException e1) {
string msg1 = e1.Message;
if (msg1.IndexOf("401") < 0) {
throw e1;
}
}
}
else {
return ix;
}
}
}
StreamReader srdr = new StreamReader(wrp.GetResponseStream());
m_browserOutput.SetContent(srdr.ReadToEnd());
//____________________________________________________________________________________________
// this method uses m_HTMLaction and m_HTMLmethod globals and returns WebResponse object
private WebResponse ReadUri(string args, string domain, string user, string password) {
WebRequest wrq = null;
WebResponse wrp = null;
string uri = m_HTMLaction;
// if it's a get append args to uri string
if (m_HTMLmethod == "get") {
uri += "?" + args;
}
//--------------------------------------------------------------------------------
// hook Fiddler up
Uri proxyURI = new Uri("http://127.0.0.1:8888");
GlobalProxySelection.Select = new WebProxy(proxyURI);
//--------------------------------------------------------------------------------
wrq = WebRequest.Create(uri);
wrq.Method = m_HTMLmethod;
wrq.ContentType = "application/x-www-form-urlencoded";
string proxy = System.Configuration.ConfigurationSettings.AppSettings["HTML_ProxyURL"];
if ((proxy != null) && (proxy != "")) {
wrq.Proxy = new WebProxy(proxy);
}
if (user != "") {
if (domain == "") {
wrq.Credentials = new NetworkCredential(user, password);
}
else {
wrq.Credentials = new NetworkCredential(user, password, domain);
}
wrq.PreAuthenticate = true;
}
// if it's a post write args to stream
if (m_HTMLmethod == "post") {
wrq.ContentLength = args.Length + 2;
StreamWriter sw = new StreamWriter(wrq.GetRequestStream());
sw.WriteLine(args);
sw.Flush();
}
wrp = wrq.GetResponse();
return wrp;
}