模仿Fiddler的Compose-> C#中的原始特性

时间:2013-03-16 03:22:31

标签: c# sockets https httpwebrequest

我正在寻找通过互联网发送原始 http字符串的最简单方式。我不想摆弄标题或cookie或内容属性,方法和所有“好”的东西。我希望它就像Fiddler一样:

enter image description here

您将整个字符串写在文本框(1)上,然后单击“执行”(2)。而且你已经完成了=利润。

我只想输入一些文字,然后发送。没有更多,仅此而已。

Socket类如果没有将我的消息发送到HTTPS服务器,那将是非常棒的,例如Fiddler没有完成任何问题。

3 个答案:

答案 0 :(得分:2)

您是否曾尝试使用System.Net.WebClient

示例来自:

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 ();
    }

答案 1 :(得分:1)

使用System.Net.Http.HttpClient Class可以很好地控制请求/响应例程,如果这是您的选项(.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))
  ...


修改:

  

...逐个设置字段......

查看HttpClient Extensions帮助您“逐个设置字段”的帮助程序

答案 2 :(得分:1)

好的,经过很多麻烦,以下是它的完成方式:

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

是的,这很容易。

http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.aspx

还有更多示例