怎么post一段数据到远程网站??????????????????????????
如题,在网上找了一段:
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();
? --------------------编程问答-------------------- http://localhost:4449/demo.aspx
改为
http://localhost/demo.aspx --------------------编程问答--------------------
晕,这是我的端口,和这个没关系。 --------------------编程问答-------------------- 重新编辑一下,为了大家看得清楚点
--------------------编程问答-------------------- http://msdn.microsoft.com/zh-cn/library/debx8sh9(VS.80).aspx#Y2570 --------------------编程问答--------------------
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);
参照你这个了,出现了和下面这个网址一样的问题,request.form也是接收不到,怎么处理这个?
http://topic.csdn.net/u/20081225/11/c059aee2-1547-470f-963d-d094f9ccc8ff.html --------------------编程问答--------------------
//发出方 (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参数,后面会报错,怎么做的啊? --------------------编程问答--------------------
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;
}
怎么这么长啊,我不用这么复杂啊,就传一个参数的呵呵。 --------------------编程问答-------------------- 7楼 子夜 写的代码看了似懂非懂的,还是道行不够。Mark! --------------------编程问答-------------------- 以上方法都试过了,怎么在目标页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呢? --------------------编程问答-------------------- 重新整理了一下发出来
//发出方 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"]
怎么都获取不到值? --------------------编程问答-------------------- 你原来的WebClient的代码就应该可以。
不知道你是如何进行测试的?在服务端debug断点一下,在Page_Load里 --------------------编程问答-------------------- mark! --------------------编程问答-------------------- 没有人做过吗? --------------------编程问答-------------------- System.Net.WebClient WebClientObj = new System.Net.WebClient();
System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();
PostVars.Add("name", name);
WebClientObj.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] byRemoteInfo = WebClientObj.UploadValues("http://localhost:4449/demo.aspx", "POST", PostVars); --------------------编程问答-------------------- 刚回答别人的帖子,把你要post的参数放在
WebRequest.Create("http://localhost:33766/target.aspx?name=12345678");中
修改一下url.
--------------------编程问答--------------------
string strData = "";
byte[] data = Encoding.UTF8.GetBytes(strData);
WebRequest request = WebRequest.Create("http://localhost:33766/target.aspx?name=12345678");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
这个不行啊 --------------------编程问答--------------------
我加上了红色部分,还是不行哦。 --------------------编程问答-------------------- 真想找到这个楼主
http://topic.csdn.net/u/20081225/11/c059aee2-1547-470f-963d-d094f9ccc8ff.html
不知道他是怎么解决的,我快死了。 --------------------编程问答-------------------- 肯定是你测试的问题,
我跑了没问题:
client 端:
var url = "http://localhost:53300/WebForm1.aspx";
var client = new WebClient();
NameValueCollection postVers = new NameValueCollection();
postVers.Add("name", "test");
var data = client.UploadValues(url, postVers);
var result = Encoding.UTF8.GetString(data);
Console.WriteLine(result);
//输出:Server get value: test
server端:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/html";
var echo = "Server get value: " + Request["name"];
Response.Write(echo);
}
}
--------------------编程问答--------------------
var url = "http://localhost:53300/WebForm1.aspx?d=youAreBadBoy";--------------------编程问答-------------------- 我的client,也就是页面http://localhost:2401/demo/default.aspx
var client = new WebClient();
NameValueCollection postVers = new NameValueCollection();
postVers.Add("name", "test");
var data = client.UploadValues(url, postVers);
var result = Encoding.UTF8.GetString(data);
Console.WriteLine(result);
//输出:Server get value: test
页面:<asp:Button id="btnQuery" OnClick="btn_click2" text="查 询" runat="server"/>
后台:
protected void btn_click2(Object sender, EventArgs e)
{
PostData();
Response.Redirect("~/target.aspx");
}
protected static void PostData()
{
var url = "http://localhost:2401/demo/target.aspx";
var client = new WebClient();
NameValueCollection postVers = new NameValueCollection();
postVers.Add("name", "test");
var data = client.UploadValues(url, postVers);
var result = Encoding.UTF8.GetString(data);
Console.WriteLine(result);
//输出:Server get value: test
}
下面是server,也就是页面http://localhost:2401/demo/target.aspx的后台:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/html";
var echo = "Server get value: " + Request["name"];
Response.Write(echo);
}
没得到name的值 --------------------编程问答-------------------- 你这是什么啊?在服务端自己Post自己,然后在 Redirect ?
你想做什么?
PostData是独立的一个客户端Request。再Redirect又是一个客户端Request
第2个Request要想在服务端有值,1.不能用Redirect,需要用Server.Transfer。
而且需要在aspx页面里放一个Hidden(id=name)
lz的asp.net看来要补一补了 --------------------编程问答-------------------- 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 ! --------------------编程问答--------------------
哦,是要建两个不同的项目吧,我是打算弄好后以后在不同的站运行。现在新增加了一个测试中。 --------------------编程问答--------------------
楼主只需要改一下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);
}
}
非常感谢,这个可以了,不过假如跨网站了还行吗? --------------------编程问答--------------------
可以。 --------------------编程问答-------------------- 我给你发的代码,就是根据这个帖子来的,我这边运行OK啊 --------------------编程问答-------------------- 虽然问题暂时都解决了,是接收方的问题。
最后有几个总结性的疑问:
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有什么区别?对我将来应用在不同网站程序之间通信有何影响? --------------------编程问答-------------------- 网上讲的:
WebClient对WebRequest进行了更高级的封装,WebClient 类使用 WebRequest 类提供对 Internet 资源的访问, --------------------编程问答--------------------
哦,难怪代码少得让我喜欢。 --------------------编程问答-------------------- 顶你个肺 --------------------编程问答-------------------- Mark! --------------------编程问答-------------------- http://blog.csdn.net/xinyaping/article/details/6270473
补充:.NET技术 , ASP.NET