Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:43:22+00:00 2026-06-07T05:43:22+00:00

I have a strange issue happening. I have some code which must do an

  • 0

I have a strange issue happening. I have some code which must do an http post to a client’s server. Their post returns content to me, which I need to show to my user. If thee is a problem, I want to trap the error and inject my own content to let the user know there was a problem.

Here is my static helper class for performing the http post.

public static class HttpPostHelper
{
    public static string Post(string url, NameValueCollection values)
    {
        StringBuilder dataBuilder = new StringBuilder();

        foreach (var key in values.AllKeys)
        {
            EncodeAndAddItem(ref dataBuilder, key, values[key]);
        }

        Uri uri = new Uri(url);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataBuilder.Length;
        using (Stream writeStream = request.GetRequestStream())
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] bytes = encoding.GetBytes(dataBuilder.ToString());
            writeStream.Write(bytes, 0, bytes.Length);
        }

        string result = String.Empty;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                {
                    result = readStream.ReadToEnd();
                }
            }
        }

        return result;
    }

    private static void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem)
    {
        if (baseRequest == null)
        {
            baseRequest = new StringBuilder();
        }
        if (baseRequest.Length != 0)
        {
            baseRequest.Append("&");
        }

        baseRequest.Append(key);
        baseRequest.Append("=");
        baseRequest.Append(System.Web.HttpUtility.UrlEncode(dataItem));
    }
}

And here is my code that does the post and returns the content.

private string GetReturnContent(string callback, NameValueCollection data, LogRequest request)
{
    string content = String.Empty;

    try
    {
        
        content = HttpPostHelper.Post(callback, data);
        
        request.Message = String.Format("Content Retrieved from Callback: {0}", content);

        logService.Debug(request);
    }
    catch (System.Net.WebException ex)
    {
        var response = ex.Response as HttpWebResponse;

        if (response != null)
        {
            if ((int)response.StatusCode == 403)
            {
                content = String.Format("The url {0} returned access denied (403).", callback);
                request.Message = content;
                logService.Warn(request);
            }
            else if ((int)response.StatusCode == 404)
            {
                content = String.Format("The url {0} returned page not found (404).", callback);
                request.Message = content;
                logService.Warn(request);
            }
            else if ((int)response.StatusCode == 405)
            {
                content = String.Format("The url {0} returned 405 error.  Please ensure the url is configured to accept http POST requests", callback);
                request.Message = content;
                logService.Warn(request);
            }
            else if ((int)response.StatusCode == 500)
            {
                content = String.Format("The url {0} returned a server error (500).", callback);
                request.Message = content;
                logService.Warn(request);
            }
            else
            {
                exceptionHandler.HandleError(ex);

                content = String.Format("The url {0} returned an error: {1}", callback, ex.Message);
                request.Message = content;
                logService.Error(request);
            }
        }
        else
        {
            exceptionHandler.HandleError(ex);

            content = String.Format("The url {0} returned an error: {1}", callback, ex.Message);
            request.Message = content;
            logService.Error(request);
        }
    }

    return content;
}

The problem is that no matter what, I always get a server error reported to me (through an e-mail reporting handler, but that is another story. I have the exception caught, and I am not re-throwing it, and yet I still get an error report.

Exception:

System.Net.WebException : Unable to connect to the remote server

Inner Exception Message:

System.Net.Sockets.SocketException : A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond [redacted]

The stack trace points to this line as the culprit in my PostHelper

using (Stream writeStream = request.GetRequestStream())

at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:43:24+00:00Added an answer on June 7, 2026 at 5:43 am

    You are getting a socket error. This error is happening before HTTP traffic is sent (hence, no status codes).

    This block is going to get executed (hence your error email notification):

    exceptionHandler.HandleError(ex);
    
    content = String.Format("The url {0} returned an error: {1}", callback, ex.Message);
    request.Message = content;
    logService.Error(request);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange issue from a client in that our code, which they
Strange issue. I have SQL Server running on an EC2 box that I can
I have a strange Behaviour issue with Sharepoint. I'm testing some javascript in the
So i have this rather strange issue, i am trying to line up some
I've got a strange issue. This is suddenly started happening in the code that
I have a strange issue (at least for me :)) with the MySQL's locking
I have a strange issue that has arisen recently: Whenever I enter text, even
I have a strange issue with jQuery's hover, addClass $(document).ready(function(){ $('#selectable li').hover( function(){ $(this).addClass('selecting',
I have a strange issue with eclipse. When I put a .xls file in
I have a really strange issue. I am working on a Java SWING application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.