Created
December 16, 2014 13:57
-
-
Save ludo6577/b77cbb7e93d7d42eb5b4 to your computer and use it in GitHub Desktop.
Simple Http Client & Server, in this example the server close the stream (tryed to answer: http://stackoverflow.com/questions/26891077/how-to-get-the-http-response-when-the-request-stream-was-closed-during-transfer)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void test(){ | |
//var request = System.Net.HttpWebRequest.CreateHttp("http://localhost:8081/index/"); | |
var request = System.Net.HttpWebRequest.Create("http://localhost:8081/index/"); | |
request.Method = "POST"; | |
string filename = "foo\u00A0bar.dat"; // Invalid characters in filename, the server will refuse it | |
request.Headers["Content-Disposition"] = string.Format("attachment; filename*=utf-8''{0}", Uri.EscapeDataString(filename)); | |
request.ContentType = "application/octet-stream"; | |
request.ContentLength = 100 * 1024 * 1024; | |
// Upload the "file" (just random data in this case) | |
try | |
{ | |
using (var stream = request.GetRequestStream()) | |
{ | |
byte[] buffer = new byte[1024 * 1024]; | |
new Random().NextBytes(buffer); | |
for (int i = 0; i < 100; i++) | |
{ | |
stream.Write(buffer, 0, buffer.Length); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
// here I get an IOException; InnerException is a SocketException | |
Console.WriteLine("Error writing to stream: {0}", ex); | |
} | |
// Now try to read the response | |
try | |
{ | |
using (var response = (HttpWebResponse)request.GetResponse()) | |
{ | |
Console.WriteLine("{0} - {1}", (int)response.StatusCode, response.StatusDescription); | |
} | |
} | |
catch (Exception ex) | |
{ | |
// here I get a WebException; InnerException is the IOException from the previous catch | |
Console.WriteLine("Error getting the response: {0}", ex); | |
var webEx = ex as WebException; | |
if (webEx != null) | |
{ | |
Console.WriteLine(webEx.Status); // SendFailure | |
var response = (HttpWebResponse)webEx.Response; | |
if (response != null) | |
{ | |
Console.WriteLine("{0} - {1}", (int)response.StatusCode, response.StatusDescription); | |
} | |
else | |
{ | |
Console.WriteLine("No response"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ServerApplication | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string[] prefixes = { "http://*:8081/index/" }; | |
SimpleListenerExample(prefixes); | |
} | |
// This example requires the System and System.Net namespaces. | |
public static void SimpleListenerExample(string[] prefixes) | |
{ | |
if (!HttpListener.IsSupported) | |
{ | |
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); | |
return; | |
} | |
// URI prefixes are required, | |
// for example "http://contoso.com:8080/index/". | |
if (prefixes == null || prefixes.Length == 0) | |
throw new ArgumentException("prefixes"); | |
while (true) { | |
// Create a listener. | |
HttpListener listener = new HttpListener(); | |
// Add the prefixes. | |
foreach (string s in prefixes) | |
{ | |
listener.Prefixes.Add(s); | |
} | |
listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; | |
listener.Start(); | |
Console.WriteLine("Listening..."); | |
// Note: The GetContext method blocks while waiting for a request. | |
HttpListenerContext context = listener.GetContext(); | |
HttpListenerRequest request = context.Request; | |
//Close the input stream but send a response ! :) | |
request.InputStream.Close(); | |
Console.WriteLine("InputStream closed! Waiting..."); | |
Thread.Sleep(2000); | |
Console.WriteLine("Send response..."); | |
// Obtain a response object. | |
HttpListenerResponse response = context.Response; | |
response.StatusCode = 400; | |
// Construct a response. | |
string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; | |
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); | |
// Get a response stream and write the response to it. | |
response.ContentLength64 = buffer.Length; | |
System.IO.Stream output = response.OutputStream; | |
output.Write(buffer, 0, buffer.Length); | |
// You must close the output stream. | |
output.Close(); | |
listener.Stop(); | |
Console.WriteLine("Closed... Do it again !"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment