C# c asmxweb服务。如何从aspx页面读取post数据
我正在使用c为我的公司编写一个web服务。所以我有一个测试页面default.aspx。 在aspx页面中,我有一个输入名=zip,我需要将其传递给webservice。 我想不出如何获得发布的数据。Webservice将基于作为xml传递的邮政编码运行sql语句。然后返回zipcodexml作为响应 代码来自下面的aspx页面C# c asmxweb服务。如何从aspx页面读取post数据,c#,web-services,C#,Web Services,我正在使用c为我的公司编写一个web服务。所以我有一个测试页面default.aspx。 在aspx页面中,我有一个输入名=zip,我需要将其传递给webservice。 我想不出如何获得发布的数据。Webservice将基于作为xml传递的邮政编码运行sql语句。然后返回zipcodexml作为响应 代码来自下面的aspx页面 string sendXml = String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>,
string sendXml = String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>,
<zip>{0}</zip>",
Request["zip"]);
string postXml = "xmlRequest=" + HttpUtility.UrlEncode(sendXml);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:51857/zipfinder.asmx/showRecords");
req.Method = "POST";
req.ContentLength = postXml.Length;
req.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(postXml);
writer.Close();
}
var result = "";
try
{
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
string getResult = reader.ReadToEnd();
Response.Write(getResult);
}
}
catch (Exception ex)
{
Response.Write("error: " + ex.ToString());
}
然后在.asmx页面上
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public DataSet getZip() {
DataSet dataSet = db.QueryDataRows("*", "zip_codes_simple", "zip = '00501' or zip = '00544'", "", "zip_codes_simple");
return dataSet;
}
[WebMethod]
public XmlDocument showRecords(){
DataSet dataSet = getZip();
string recs = "";
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);
XmlNode root = doc.CreateElement("zipcodes");
doc.AppendChild(root);
XmlElement zip = doc.CreateElement("zip");
XmlElement type = doc.CreateElement("type");
XmlElement primaryCity = doc.CreateElement("primaryCity");
XmlElement state = doc.CreateElement("state");
XmlElement latitude = doc.CreateElement("latitude");
XmlElement longitude = doc.CreateElement("longitude");
foreach (DataTable dt in dataSet.Tables)
{
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
zip.InnerText = dr["zip"].ToString();
root.AppendChild(zip);
type.InnerText = dr["type"].ToString();
root.AppendChild(type);
primaryCity.InnerText = dr["primary_city"].ToString();
root.AppendChild(primaryCity);
state.InnerText = dr["state"].ToString();
root.AppendChild(state);
latitude.InnerText = dr["latitude"].ToString();
root.AppendChild(latitude);
longitude.InnerText = dr["longitude"].ToString();
root.AppendChild(longitude);
//recs = "<p>" + dr["zip"] + ", " + dr["primary_city"] + "</p>";
//Response.Write("<p>" + dr["zip"] + " in " + dr["state"]);
}
}
else
{
//recs = "<p style='color: red;'>Invalid user name or password</p>";
}
}
return doc;
}
如何从aspx页面获取邮政编码以传递到zip=
我正在寻找一些东西来读取格式化字符串,并从传入的xml文档中提取邮政编码
谢谢你的帮助。
谢谢如果我理解正确,您希望从页面中的获取值。由于它不是一个asp:TextBox,您应该使用它来获取它,因此您的代码如下所示:
string sendXml = String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>,
<zip>{0}</zip>",
Request.Form["zip"]);
但还有另一个解决方案:与其这样做:
<input type='text' name='zip' />
您可以使用ASP.NET Web窗体控件:
<asp:TextBox runat='server' ID='zip' />
然后您可以使用zip.Text获取值
有什么理由不这样做吗?是的,这是因为我们的cms不支持内联服务器标记。在asmx文件中是否有一种方法可以调用xml邮政编码。例如在aspx中,我可以使用Request[field],但是,我还没有看到一种引用xml的方法。基本上我需要一些东西来读取xml,读取一个字段并使用sql语句中的值?如果没有,请尝试Request.Form[zip]。要从XML文档中提取信息,可以使用xPath,例如/zipcodes/zip将匹配zipcode中的所有zip。作为变量字符串getResult=reader.ReadToEnd;包含响应XML,您可以将其加载到XmlDocument中,然后使用doc.SelectNodes/xpath/to/node执行反查询。我不确定我是否理解你的问题。