怎么post一段数据到远程网站??????????????????????????

ProgrammerTopLee 2011-06-11 07:56:55
如题,在网上找了一段:

System.Net.WebClient WebClientObj = new System.Net.WebClient();
System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();
PostVars.Add("name", name);
byte[] byRemoteInfo = WebClientObj.UploadValues("http://localhost:4449/demo.aspx", "POST", PostVars);


为什么在demo.aspx接收不到参数Request.Form["name"].ToString();
...全文
985 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
YapingXin 2011-07-26
  • 打赏
  • 举报
回复
http://blog.csdn.net/xinyaping/article/details/6270473
Yushangyuan 2011-07-26
  • 打赏
  • 举报
回复
Mark!
lyglary 2011-06-14
  • 打赏
  • 举报
回复
顶你个肺
patrickjiang 2011-06-12
  • 打赏
  • 举报
回复
7楼 子夜 写的代码看了似懂非懂的,还是道行不够。Mark!
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxr0323 的回复:]
C# code
const string sUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
const string sContentType = "application/x-www-form-urlencoded"……
[/Quote]
怎么这么长啊,我不用这么复杂啊,就传一个参数的呵呵。
子夜__ 2011-06-12
  • 打赏
  • 举报
回复
const string sUserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
const string sContentType = "application/x-www-form-urlencoded";
const string sRequestEncoding = "utf-8";
const string sResponseEncoding = "utf-8";

/// <summary>
/// Post data到url
/// </summary>
/// <param name="data">要post的数据</param>
/// <param name="url">目标url</param>
/// <returns>服务器响应</returns>
public static string PostDataToUrl(string data, string url)
{
Encoding encoding = Encoding.GetEncoding(sRequestEncoding);
byte[] bytesToPost = encoding.GetBytes(data);
return PostDataToUrl(bytesToPost, url);
}

/// <summary>
/// Post data到url
/// </summary>
/// <param name="data">要post的数据</param>
/// <param name="url">目标url</param>
/// <returns>服务器响应</returns>
public static string PostDataToUrl(byte[] data, string url)
{
#region 创建httpWebRequest对象
WebRequest webRequest = WebRequest.Create(url);
HttpWebRequest httpRequest = webRequest as HttpWebRequest;
if (httpRequest == null)
{
throw new ApplicationException(
string.Format("Invalid url string: {0}", url)
);
}
#endregion

#region 填充httpWebRequest的基本信息
httpRequest.UserAgent = sUserAgent;
httpRequest.ContentType = sContentType;
httpRequest.Method = "POST";
#endregion

#region 填充要post的内容
httpRequest.ContentLength = data.Length;
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
#endregion

#region 发送post请求到服务器并读取服务器返回信息
Stream responseStream;
try
{
responseStream = httpRequest.GetResponse().GetResponseStream();
}

catch (Exception e)
{
// log error WinForm调试方式
//Console.WriteLine(
// string.Format("POST操作发生异常:{0}", e.Message)
// );
throw e;
}
#endregion

#region 读取服务器返回信息
string stringResponse = string.Empty;
using (StreamReader responseReader =
new StreamReader(responseStream, Encoding.GetEncoding(sResponseEncoding)))
{
stringResponse = responseReader.ReadToEnd();
}
responseStream.Close();
#endregion
return stringResponse;
}

/// <summary>
/// 将字符编码为Base64
/// </summary>
/// <param name="encodeType">编码方式</param>
/// <param name="input">明文字符</param>
/// <returns>字符串</returns>
public static string EncodeBase64(string encodeType, string input)
{
string result = string.Empty;
byte[] bytes = Encoding.GetEncoding(encodeType).GetBytes(input);
try
{
result = Convert.ToBase64String(bytes);
}
catch
{
result = input;
}
return result;
}
/// <summary>
/// 将字符编码为Base64
/// </summary>
/// <param name="encodeType">编码方式</param>
/// <param name="input">明文字符</param>
/// <returns>字符串</returns>
public static string DecodeBase64(string encodeType, string input)
{
string decode = string.Empty;
byte[] bytes = Convert.FromBase64String(input);
try
{
decode = Encoding.GetEncoding(encodeType).GetString(bytes);
}
catch
{
decode = input;
}
return decode;
}
haojuntu 2011-06-12
  • 打赏
  • 举报
回复
mark!
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复

//发出方 (window service 的代码)
//string strData = "?usernumber=" + strNumber + "&UserName=" + strUserName;
//byte[] data = Encoding.UTF8.GetBytes(strData);
WebRequest request = WebRequest.Create("http://192.168.1.104/Send/SendMsg.aspx"
?usernumber=" + strNumber + "&UserName=" + strUserName);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream MyStream = request.GetRequestStream();
MyStream.Write(data, 0, data.Length);
MyStream.Close();

按照这个写法注释掉了strData和data参数,后面会报错,怎么做的啊?
机器人 2011-06-12
  • 打赏
  • 举报
回复
你原来的WebClient的代码就应该可以。

不知道你是如何进行测试的?在服务端debug断点一下,在Page_Load里
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复
重新整理了一下发出来
//发出方 http://localhost:2401/demo/send.aspx
string strData = "?name=1234";
byte[] data = Encoding.UTF8.GetBytes(strData);
WebRequest request = WebRequest.Create("http://localhost:2401/demo/target.aspx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream MyStream = request.GetRequestStream();
MyStream.Write(data, 0, data.Length);
MyStream.Close();

//接收方 http://localhost:2401/demo/target.aspx
Page_Load事件使用Request.Form["name"]、Request.Params["name"]、Request["name"]
怎么都获取不到值?
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复
以上方法都试过了,怎么在目标页Page_Load事件使用Request.Form["name"]、Request.Params["name"]、Request["name"]都获取不到值,但是使用下面的代码

using (StreamReader readStream = new StreamReader(Request.InputStream, Encoding.Default))
{
result = readStream.ReadToEnd();Request.Params
}
Response.Write(result);

获取到了以下数据
__VIEWSTATE=%2FwEPDwUKLTczMTE0NTI3OWRkQb7WNNpV5yLHeB5VpqMI8CB%2FmHI%3D&name=TestName&btnQuery=%E6%9F%A5++%E8%AF%A2&__EVENTVALIDATION=%2FwEWAwLRldzOBQLiu6%2BxBgLvjry%2FBctDYzysEzEhznIVeqWT%2B92pPSXC

怎么用Request直接获取参数name呢?
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 chengzq 的回复:]
网上讲的:
WebClient对WebRequest进行了更高级的封装,WebClient 类使用 WebRequest 类提供对 Internet 资源的访问,
[/Quote]
哦,难怪代码少得让我喜欢。
chengzq 2011-06-12
  • 打赏
  • 举报
回复
网上讲的:
WebClient对WebRequest进行了更高级的封装,WebClient 类使用 WebRequest 类提供对 Internet 资源的访问,
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复
虽然问题暂时都解决了,是接收方的问题。
最后有几个总结性的疑问:

static string strTempFromDefaultPage;
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/html";
if (string.IsNullOrEmpty(strTempFromDefaultPage))
{
strTempFromDefaultPage = Request["name"];
}
else
{
var echo = "Server get value: " + strTempFromDefaultPage;
Response.Write(echo);
}
}

1、这上面的参数strTempFromDefaultPage起什么作用啊(之前没有都不可以)?
2、WebRequest和WebClient有什么区别?对我将来应用在不同网站程序之间通信有何影响?
chengzq 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 programmertoplee 的回复:]

真想找到这个楼主
http://topic.csdn.net/u/20081225/11/c059aee2-1547-470f-963d-d094f9ccc8ff.html
不知道他是怎么解决的,我快死了。
[/Quote]我给你发的代码,就是根据这个帖子来的,我这边运行OK啊
aaseh 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 programmertoplee 的回复:]
引用 26 楼 aaseh 的回复:
引用 22 楼 programmertoplee 的回复:
我的client,也就是页面http://localhost:2401/demo/default.aspx
页面:<asp:Button id="btnQuery" OnClick="btn_click2" text="查 询" runat="server"/>
后台:

C# code
……
[/Quote]
可以。
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 aaseh 的回复:]
引用 22 楼 programmertoplee 的回复:
我的client,也就是页面http://localhost:2401/demo/default.aspx
页面:<asp:Button id="btnQuery" OnClick="btn_click2" text="查 询" runat="server"/>
后台:

C# code

protected void bt……
[/Quote]
非常感谢,这个可以了,不过假如跨网站了还行吗?
aaseh 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 programmertoplee 的回复:]
我的client,也就是页面http://localhost:2401/demo/default.aspx
页面:<asp:Button id="btnQuery" OnClick="btn_click2" text="查 询" runat="server"/>
后台:

C# code

protected void btn_click2(Object sender, EventAr……
[/Quote]
楼主只需要改一下target.aspx的代码即可:
        static string strTempFromDefaultPage;
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/html";
if (string.IsNullOrEmpty(strTempFromDefaultPage))
{
strTempFromDefaultPage = Request["name"];
}
else
{
var echo = "Server get value: " + strTempFromDefaultPage;
Response.Write(echo);
}
}
ProgrammerTopLee 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 fangxinggood 的回复:]
WebClient 是给客户端模拟浏览器访问服务端请求用的。

本来都在一个asp.net application的页面传值问题,被你搞错方向了!

asp.net 页面传值,考虑用

方法1. Hidden -- Request["Key"] 取值 (Server.Transfer("target.aspx"))
方法2. QueryString -- Request.QueryS……
[/Quote]
哦,是要建两个不同的项目吧,我是打算弄好后以后在不同的站运行。现在新增加了一个测试中。
机器人 2011-06-12
  • 打赏
  • 举报
回复
WebClient 是给客户端模拟浏览器访问服务端请求用的。

本来都在一个asp.net application的页面传值问题,被你搞错方向了!

asp.net 页面传值,考虑用

方法1. Hidden -- Request["Key"] 取值 (Server.Transfer("target.aspx"))
方法2. QueryString -- Request.QueryString["Key"] 取值 (Response.Redirect("target.aspx?Key=xxx"))
方法3. 直接用 Session !
加载更多回复(15)

62,052

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧