首页 新闻 会员 周边 捐助

c# winform tcpclient 链接代理

0
悬赏园豆:30 [待解决问题]

c# winform tcpclient 链接代理

 

private void button1_Click(object sender, EventArgs e)
{
TcpClient tcpclicnet = new TcpClient();
string ip = textBox1.Text.Trim();
//IPAddress ip = IPAddress.Parse(textBox1.Text.Trim()); //代理IP
int tcpport = int.Parse(textBox2.Text.Trim()); //代理IP 端口
//IPEndPoint ipend = new IPEndPoint(ip,tcpport);
tcpclicnet.Connect(ip,tcpport);
string requeststr = string.Empty;
if (tcpclicnet.Connected)
{
requeststr = string.Format("CONNECT {0}:{1} HTTP/1.0\r\n\r\n", ip, tcpport.ToString());
try
{
textBox3.Text=requeststr;
tcpclicnet.Client.Send(Encoding.UTF8.GetBytes(requeststr));
using (NetworkStream stream = tcpclicnet.GetStream())
{
using (StreamReader reading = new StreamReader(stream))
{
string content = null;
while ((content = reading.ReadLine()) != null)
{
textBox3.Text+=content;
if (string.IsNullOrEmpty(content))
{
break;
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}

}

}

 

 

以上是我写的代码

返回的信息是
HTTP/1.0 403 ForbiddenDate: Mon, 24 Feb 2014 18:27:05 GMTContent-Length: 257Content-Type: text/htmlServer: NetCache appliance (NetApp/6.0.7)Connection: keep-aliveProxy-Connection: keep-alive

相同的IP代理在QQ上设置代理测试可以链接,我这里哪里写的不对呢?

问题补充:

我是从网上搜索的代理,提供的格式是 

112.124.41.71:8088
121.199.62.105:8088
112.124.38.227:8088

amyeeq的主页 amyeeq | 初学一级 | 园豆:77
提问于:2014-02-25 02:31
< >
分享
所有回答(1)
0

HTTP 代理服务器:255.255.255.255 8888

URL:http://q.cnblogs.com/q/59728/

 

TCP Connect 到 255.255.255.255:8888 

 

通过 HTTP 代理发送出去的 HTTP 请求的格式应该是这样的:

GET http://q.cnblogs.com/q/59728/ HTTP/1.1
Host: q.cnblogs.com

Launcher | 园豆:45050 (高人七级) | 2014-02-25 09:24

谢谢您的回复

我是从网上搜索的代理,提供的格式是 

112.124.41.71:8088
121.199.62.105:8088
112.124.38.227:8088

 

如果我请求 smtp.163.com:25 应该怎么写呢?

支持(0) 反对(0) amyeeq | 园豆:77 (初学一级) | 2014-02-26 01:18

@amyeeq: 请问,你是使用 HTTP 代理发送 HTTP 请求吗?

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2014-02-26 09:07

@Launcher: 用http代理发邮件

支持(0) 反对(0) amyeeq | 园豆:77 (初学一级) | 2014-02-26 10:11

@amyeeq: 

tcpclicnet.Connect("112.124.41.71",8088);  // 建立到 HTTP 代理服务器的连接

NetworkStream stream = tcpclicnet.GetStream(); // 获取流,准备向代理服务器写入请求

stream.Write("CONNECT smtp.163.com:25 HTTP/1.1");  // 写入希望连接 SMTP 服务器的指令。

stream.Read(); // 读取响应

如果响应为 HTTP 200,那么你就可以使用 stream 开始写入 SMTP 指令了。

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2014-02-26 10:39

@Launcher: 修改后代码

private void button1_Click(object sender, EventArgs e)
{
TcpClient tcpclicnet = new TcpClient();
string ip = textBox1.Text.Trim();
int tcpport = int.Parse(textBox2.Text.Trim());
tcpclicnet.Connect(ip,tcpport);
string requeststr = string.Empty;
if (tcpclicnet.Connected)
{
requeststr = string.Format("CONNECT {0}:{1} HTTP/1.1", "smtp.163.com", 25);
try
{
textBox3.Text=requeststr;
byte[] bWrite = Encoding.Default.GetBytes(requeststr.ToCharArray());
NetworkStream stream = tcpclicnet.GetStream();
WriteToNetStream(ref stream, requeststr, false);
string reading=ReadFromNetStream(ref stream);
string content = reading;
textBox3.Text += content;
}
catch (Exception ex)
{
throw ex;
}

}

}

/// <summary>
/// 向网络数据流中写入指令
/// </summary>
/// <param name="NetStream"></param>
/// <param name="message"></param>
/// <param name="isNewLine"></param>
private void WriteToNetStream(ref NetworkStream NetStream, string message, bool isNewLine)
{
string stringToSend = isNewLine ? message + "\r\n" : message;

//将命令行转化为byte[]
byte[] bWrite = Encoding.Default.GetBytes(stringToSend.ToCharArray());

int start = 0;
int length = bWrite.Length;
int page = 0;
int size = 2048;
int count = size;
try
{
if (length > size)
{
//数据分页
if ((length / size) * size < length)
page = length / size + 1;
else
page = length / size;

for (int i = 0; i < page; i++)
{
start = i * size;
if (i == page - 1)
count = length - (i * size);

NetStream.Write(bWrite, start, count);//将数据写入到服务器上
}
}
else
NetStream.Write(bWrite, 0, bWrite.Length);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 读取网络流中的反馈
/// </summary>
/// <param name="NetStream"></param>
/// <returns></returns>
private string ReadFromNetStream(ref NetworkStream NetStream)
{
byte[] rev = new byte[1024];
NetStream.Read(rev, 0, rev.Length);

return Encoding.Default.GetString(rev);
}

返回:

CONNECT smtp.163.com:25 HTTP/1.1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<h1>400 Bad Request</h1>
<p>Your browser sent a request that this server could not understand. Sorry for the inconvenience.<br/>
Please report this message and include the following information to us.<br/>
Thank you very much!</p>
<table>
<tr>
<td>URL:</td>
<td>http://localhost:8088</td>

可是用112.124.41.71:8088代理上网可以上网,ip138显示也是代理的ip

支持(0) 反对(0) amyeeq | 园豆:77 (初学一级) | 2014-02-26 11:35

@amyeeq: 我找了篇教程,自己琢磨吧,http://blog.csdn.net/luyifeiniu/article/details/3320651

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2014-02-26 11:38

@Launcher: 谢谢您,可能我刚才回复的长了,主要问题是代理IP可以上网,可是发smtp指命令返回的是:

00 Bad Request ,一直找不到原因,请指点。

支持(0) 反对(0) amyeeq | 园豆:77 (初学一级) | 2014-02-26 11:50

@amyeeq: 如果你发送的指令正确,那么返回 Bad Request,就是表示代理服务器不识别此指令。你需要找个一个能识别此指令的代理。

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2014-02-26 13:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册