【发布时间】:2009-09-04 16:50:29
【问题描述】:
我需要完成以下任务并需要以下 #2 的帮助
- 我的站点有一个带有表单的页面,提交的表单数据需要写入我站点上的数据库。
- 在写入数据库后,表单上提交的相同数据需要发送到另一个站点上处理它的页面,以便表单提交来自该其他站点上的页面。在另一个站点上处理它的页面是一个 php 页面。
【问题讨论】:
-
您是否可以控制#2 中其他站点上的 PHP?
我需要完成以下任务并需要以下 #2 的帮助
【问题讨论】:
有点不清楚,但我的猜测是,在将数据写入数据库后,您正试图向另一个 .php 页面进行“表单发布”。
您可以从this wonderful Scott Hanselman article 获得更多信息,但这里是摘要:
public static string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy = new System.Net.WebProxy(ProxyString, true);
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream ();
os.Write (bytes, 0, bytes.Length); //Push it out there
os.Close ();
System.Net.WebResponse resp = req.GetResponse();
if (resp== null) return null;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
【讨论】:
您的问题的理想解决方案是在 php 站点上创建一个 Web 服务,然后您的 asp.net 代码调用该 Web 服务。 http://en.wikipedia.org/wiki/Web_service
在 PHP 中创建 Web 服务:http://www.xml.com/pub/a/ws/2004/03/24/phpws.html
在 ASP.Net 中调用 Web 服务:http://www.codeproject.com/KB/webservices/WebServiceConsumer.aspx
或者,您可以创建一个从您的 asp.net 到 php 站点的 http 请求,将所有表单元素发布到 php 站点。
这里是一个例子:http://www.netomatix.com/httppostdata.aspx
注意:从中长期来看,你几乎肯定会遇到第二种方法的问题,除非你无法控制 php 站点,否则我不推荐它。
【讨论】: