C Sharp使用HTTP代码示例

以下是使用C#发送HTTP请求的示例代码:

```csharp

using System;

using System.Net;

using System.IO;

class Program

{

static void Main(string[] args)

{

// 创建一个Web请求对象

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("Example Domain");

// 设置请求方法为GET

request.Method = "GET";

// 发送请求并获取响应

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// 读取响应内容

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{

string responseText = reader.ReadToEnd();

Console.WriteLine(responseText);

}

}

}

```

这个示例代码使用了`HttpWebRequest`和`HttpWebResponse`类来发送HTTP请求和获取响应。在创建`HttpWebRequest`对象时,需要指定请求的URL,并设置请求方法。在发送请求后,可以通过`HttpWebResponse`对象获取响应,并使用`StreamReader`类读取响应内容。

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ip.hahado.cn/ip");

    WebProxy myProxy = new WebProxy();

    Uri newUri = new Uri("http://ip.hahado.cn:39010");

    myProxy.Address = newUri;

    myProxy.Credentials = new NetworkCredential("username", "password");


    request.Proxy = myProxy;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值