【发布时间】:2010-10-27 07:56:51
【问题描述】:
我在使用 HttpWebRequest 发布 xml 数据时遇到了一些问题。其实我想发布 3 个表单变量
其中2个用于凭证,第三个用于将XML数据发送到api,api将验证并处理xml数据,如果没有发现错误则返回成功。
这是文档所说的内容。
数据将通过 HTTPS FORM post 传递到网关,成功接收数据后将返回字符串值“success”。总共将发布三个 FORM 变量,其中两个将包含凭据,第三个将包含 HR-XML 数据。如果由于任何原因发布失败,将返回一个字符串值“error”。
表单域 integration_field1 = 1234, integration_field2=2345pwd, hrxml=包含 HR-XML 顺序的表单字段(字符串)
这是我编码的..
string strId = "123";
string strName = "test";
StreamReader sr2 = File.OpenText(Server.MapPath("~/App_Data/XMLFile.xml"));
string xml = sr2.ReadToEnd();
sr2.Close();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
xml = HttpUtility.UrlEncode(xmlDoc.OuterXml);
System.Text.UTF8Encoding encoding = new UTF8Encoding();
string postData = "xmldata=" + xml;
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost:1994/TestXMLPost/PostData.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
WebResponse res = (HttpWebResponse)myRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string str = sr.ReadToEnd();
sr.Close();
在 PostData.aspx 上,我这样做是为了测试
if (Request.Form.Count > 0)
{
Response.Write( Request.Form["xmldata"] + "");
}
我没有收到任何回复。只有错误“远程服务器返回错误:(500)内部服务器错误。” 这是我想使用 HttpWebRequest 模拟的 HTML 表单。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>testing</title>
</head>
<body>
<form action="https://myurl.com/samplepage.cfm" method="post" >
integration_id<input type="text" name="integration_id" value="id" /><br />
integration_pwd<input type="text" name="integration_pwd" value="pwd" /><br />
<textarea name="hrxml" style="height: 404px; width: 593px">
<?xml version="1.0" encoding="UTF-8"?>
<BackgroundCheck account="test" userId="test" password="test">
<BackgroundSearchPackage>
<ReferenceId>
<IdValue>12345678</IdValue>
</ReferenceId>
<PersonalData>
<PersonName>
<GivenName>TEST</GivenName>
<FamilyName primary="undefined">TEST</FamilyName>
</PersonName>
<DemographicDetail>
<GovernmentID countryCode="US" issuingAuthority="SSN">00000000000000</GovernmentID>
<DateOfBirth>1901-01-01</DateOfBirth>
</DemographicDetail>
</PersonalData>
</BackgroundSearchPackage>
</BackgroundCheck>
</textarea>
<input type="submit" />
</form>
</body>
</html>
谢谢
【问题讨论】:
-
...和?有什么问题?你试过什么?
标签: asp.net xml forms post httpwebrequest