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 8370417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:54:24+00:00 2026-06-09T13:54:24+00:00

I need to locally test some code that uses a WebClient object for communicating

  • 0

I need to locally test some code that uses a WebClient object for communicating with an http server. For that purpose, I decided to write an incredibly simplistic mock server that reads some bytes from a binary file and sends them to the client. Here is the relevant server’s code:

public void Listen()
{
    TcpClient client;
    NetworkStream netStream;
    listener.Start();

    try
    {
        client = listener.AcceptTcpClient();
        netStream = client.GetStream();
        ReadData(netStream);
        Console.WriteLine("Writing data, sequence 1");
        WriteData(netStream, 1);
        netStream.Close();
        client.Close();

        client = listener.AcceptTcpClient();
        netStream = client.GetStream();
        ReadData(netStream);
        Console.WriteLine("Writing data, sequence 2");
        WriteData(netStream, 2);
        netStream.Close();
        client.Close();
    }
    catch(Exception e)
    {
    }
}

private void WriteData(NetworkStream netStream, int sequence)
{
    byte[] bytes = PrepareResponse(sequence);    //get data from a file
    netStream.Write(bytes, 0, bytes.Length);
}

With WebClient I use UploadString:

string response = client.UploadString(address + link, data);

So, the thing is, the WebClient apparently receives all the sent data, but throws a System.IO.Exception (“Unable to read data from the transport connection: The connection was closed.”) when I close the stream server-side. If I don’t close the stream, WebClient blocks and waits for the transfer to finish, despite all the data being received. I tried to substitute WebClient with WebRequest, and it also either throws an exception if the socket was closed server-side or blocks on stream.Read after reading all the data. What I find strange is that the first block of data I send to WebClient is an http 302 “Moved” response, which causes the WebClient to immediately send a GET request. Despite the fact that I send data identically two times, WebClient throws an exception only on the second response.

So – I need a way to correctly send the data and close the connection so it works with WebClient, and I would also like to know why does WebClient correctly handle the 302 response and throw an exception on the second response.

Thank you.

  • 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-09T13:54:26+00:00Added an answer on June 9, 2026 at 1:54 pm

    The answer was actually quite trivial – the second http response had “Transfer-Encoding: chunked” set in its headers, and I didn’t actually send the terminating zero-length chunk at the end of the response, thus WebClient was waiting for more data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store locally on android devices some images I get from the
Locally I need to code against Java 1.4. I tried the suggestion in this
I need to run some classic ASP locally before deployment to a legacy web
I need to download an XML file, using HTTP protocol, to use it locally
I'm running my own LAMP server locally. Something i need to setup? Should it
I'm trying to write some test cases for my Qooxdoo application, and I think
So I am working on a test app that downloads files locally to the
I need to be able to display html files that are stored locally on
Can we implement Facebook applications and test it locally ? or we need to
I need to test that my Django application sends e-mails with correct content. I

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.