1

I'm looking for the simplest way to send a raw http string over the internet. I don't want to fiddle with header or cookie or contents properties, with methods and all the "nice" things there is. I want it to be just like Fiddler is:

enter image description here

You write your whole string on a textbox (1) and then click "Execute" (2). And you're done = PROFIT.

I just want to type some text, and have it sent. Nothing more, nothing less.

The Socket class would be awesome if it didn't fail to send my messages to HTTPS servers, which is something that Fiddler, for instance, has no problem accomplishing.

devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • You could always make an extension method. – mbeckish Mar 16 '13 at 03:30
  • I was just wondering if there's a direct method. The idea is precisely to not have to write that extension method :( – devoured elysium Mar 16 '13 at 03:34
  • You know that you'd only have to write it once in your entire career, and you could reuse it forever after. :) – mbeckish Mar 16 '13 at 03:35
  • I could just as well never have to write it in the first place if there's already a method that does it. – devoured elysium Mar 16 '13 at 03:37
  • Yes, if it existed, you wouldn't have to write it. Quick way to check: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest_methods.aspx – mbeckish Mar 16 '13 at 03:38
  • You might like the [WebClient](http://msdn.microsoft.com/en-us/library/tt0f69eh.aspx) class better. – mbeckish Mar 16 '13 at 03:42
  • But `HttpWebRequest` doesn't have a `Request` property. What exactly are you trying to do? Or avoid doing? – Jim Mischel Mar 16 '13 at 03:46
  • The given code is what I was expecting to find, not what actually exists. – devoured elysium Mar 16 '13 at 03:49
  • possible duplicate of [Easiest way to read from a URL into a string in .NET](http://stackoverflow.com/questions/1048199/easiest-way-to-read-from-a-url-into-a-string-in-net) – mbeckish Mar 16 '13 at 03:59
  • That has *nothing* to do with the supposed duplicate. I'm asking how to send an HTTP request over a HTTPS connection without having to manually set every header. – devoured elysium Mar 16 '13 at 04:20
  • 1
    What headers do you think you'll need to set? The standard headers are set when you create an `HttpWebRequest`. If there's something specific that your server needs and that `HttpWebRequest` doesn't supply, then of course you'll have to set it yourself. – Jim Mischel Mar 16 '13 at 04:37
  • @Jim Mischel: When using Fiddler, I can go to the "Composer"->"Raw" and then write whatever I want there and click "execute". I'm looking for exactly the same functionality in C#. I don't want to have to manually fiddle with properties and methods, I just want to send the full http request as a string and to be done with it. – devoured elysium Mar 17 '13 at 15:31
  • That functionality doesn't exist in `HttpWebRequest` or any other Framework class that I'm aware of. You'll have to write a wrapper method or find one that somebody else has written. – Jim Mischel Mar 17 '13 at 18:20

3 Answers3

2

Have you ever try using System.Net.WebClient?

Example from:

using System;
using System.Net;
using System.IO;

public class Test
{
    public static void Main (string[] args)
    {
        if (args == null || args.Length == 0)
        {
            throw new ApplicationException ("Specify the URI of the resource to retrieve.");
        }
        WebClient client = new WebClient ();

        // Add a user agent header in case the  
        // requested URI contains a query.

        client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        Stream data = client.OpenRead (args[0]);
        StreamReader reader = new StreamReader (data);
        string s = reader.ReadToEnd ();
        Console.WriteLine (s);
        data.Close ();
        reader.Close ();
    }
Peter PAD
  • 2,252
  • 1
  • 17
  • 20
  • I don't to have to set the headers manually. I just want to type in the whole text and then have it sent. See the edited post above. – devoured elysium Mar 17 '13 at 15:47
1

You get a good control over the request/response routine using the System.Net.Http.HttpClient Class, if that's an option for you (.NET 4.5):

string url = "https://site.com";
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
  var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
  var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
  ...


Edit:

... settings the fields one by one ....

Look at the HttpClient Extensions for helpers that could aid you in "setting the fields one by one"

G. Stoynev
  • 7,389
  • 6
  • 38
  • 49
1

Ok, after a lot of hassle, here's how it's done:

var tcpClient = new TcpClient(hostName, port);
var stream = new SslStream(tcpClient.GetStream(), false, (sender, certificate, chain, errors) => true, null); //little hack
stream.AuthenticateAsClient(hostName);
//from now on you may write your usual stuff on the stream

Yes, it's that easy.

There are more examples at http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.aspx

devoured elysium
  • 101,373
  • 131
  • 340
  • 557