Using WebRequest... what the heck is wrong?

I'm changing credit card gateways, using I whipped up a class that I can use on all my sites, the key method being this one:

public void Submit()
    string postElements = "?x_login=" + _login
        + etc...;
    WebRequest request =
 + postElements);
    request.Method = "POST";
    request.Timeout = 60000;
    WebResponse response = request.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream());
    string result = sr.ReadToEnd();
    _rawResult = result;
    string[] results = result.Split(new char[] {'|'});
    if (results[0] == "1") _approved = true;
    _errorText = results[3];
    _avs = results[5];
    _transactionID = results[6];

This works great from a unit test that calls it something like this:
Transaction t = new Transaction();
t.Address = "3412 Beaumont Dr.";

But when I try to use the exact same code in my Web app, it ALWAYS times out on the request.GetResponse() call. What gives? How is that possible? It times out both on my own box (the one that runs the unit test fine) and on my production server.


  • Have you tried using a username&password to see if it is a security problem?

    A dummy html page instead of the real url?


    You don't need new char[]{} , its a param array ;)

    string[] results = result.Split('|');

  • Also, pay attention to IDisposable. HttpWebResponse implements IDisposable.

  • So here's something I can't explain...

    Originally I was using the same old code I've used anywhere by passing in the "form fields" for the POST like this:

    request.ContentType = "application/x-www-form-urlencoded";

    byte[] bRequest = Encoding.ASCII.GetBytes(postElements);

    request.ContentLength = bRequest.Length;

    Stream s = request.GetRequestStream();

    s.Write(bRequest, 0, bRequest.Length);

    ...then calling GetResponse(). That wasn't working in the unit test, so I just tacked on the query string (which it later occured to me isn't secure).

    So I went back to the original code writing bytes to the request. Again, it failed in the unit test, but for reasons I can't explain, it worked perfectly from the Web app.

Comments have been disabled for this content.