c# .Net webrequest issue

I have two questions about "under the hood" processes executed by Webrequest and the getresponse method.

1) I have noticed while packet capturing that, when doing a webrequest with a payload, packets starting a TCP/IP connection are sent and received *before* the getresponse method?  I would have expected the payload to be cached and sent only when the getresponse was called?

2) I have a webrequest case that has me puzzled.  It is an HTTPS URI.  All proceeds as expected (443 TCP/IP connection made before getresponse called) until the getresponse is called.  When that happens, the process attempts to create a new TCP/IP connection to the server, this one being on port 80 (not HTTPS).  Since the request is not HTTPS, the server ignores it and the process times out with a "no response from server".

What would cause the getresponse method to spawn a new connection, and why would it send it via HTTP when the original connection was made via HTTPS?  The certificate for the server is valid, and I have tried turning off the check-certificate functionality.

Thanks for any insight on both questions.

Bryan Hunt
Who is Participating?
Bill BachPresidentCommented:
Can you post the source code snippet? I wonder if a second (new) object is being used because of a typo, such as improper case. I've done this before myself, and errors like that can be hard to find.
brhuntAuthor Commented:
Here you go Bill:

WebRequest geocodeRequest = WebRequest.Create("https://apps.abc123.com/356A8F39-F50D-4248-8D94-67EF369A4AD7/APP/REST/Geocode");
geocodeRequest.Method = "POST";
geocodeRequest.ContentType = "application/json";
geocodeRequest.Headers.Add("oitoken", securityToken);

GeocodeRequest myGReq = new GeocodeRequest
    keyCustomer = "A Site",
    keyString = "A key",
    addressList = new string[] { "123 W. Main St., Anywhere, TX 12345" }

string a = JsonConvert.SerializeObject(myGReq);
byte[] byteArray = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(myGReq));
geocodeRequest.ContentLength = byteArray.Length;
Stream dataStream = geocodeRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);

WebResponse geocodeResponse = geocodeRequest.GetResponse();
dataStream = geocodeResponse.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string geocodeResponseFromServer = reader.ReadToEnd();

GeocodeResponse myGResp = JsonConvert.DeserializeObject<EZTGeocodeResponse>(geocodeResponseFromServer);

All Courses

From novice to tech pro — start learning today.