ASMX和WCF服务调用的HTTP头是否相同?
我们正在尝试以编程方式使用第三方安全WCF服务。我试图通过编程方式添加soap请求参数和HTTP头。是否有必须或标准的字符串/值对添加到标题中?目前,我们正在做一些类似于构建自定义soap请求的事情:ASMX和WCF服务调用的HTTP头是否相同?,wcf,soap,http-headers,server-error,Wcf,Soap,Http Headers,Server Error,我们正在尝试以编程方式使用第三方安全WCF服务。我试图通过编程方式添加soap请求参数和HTTP头。是否有必须或标准的字符串/值对添加到标题中?目前,我们正在做一些类似于构建自定义soap请求的事情: public static string CustomSoapReq(string WSDL_URL) { string strCustomModels = string.Empty; try { System.Net.Cookie SMCookie;
public static string CustomSoapReq(string WSDL_URL)
{
string strCustomModels = string.Empty;
try
{
System.Net.Cookie SMCookie;
CookieCollection IFCookies;
string successfuluserid = String.Empty;
ClsGFWUtility clsutill;
HttpWebRequest httpReq;
SMCookie = new Cookie();
IFCookies = new CookieCollection();
clsutill = new ClsGFWUtility(); //inbuilt function to get the cookie information
IFCookies = clsutill.GetCookie("LogonUrl", "IFLoginUserId", "IFLoginPassword", out successfuluserid);
if (IFCookies != null && successfuluserid.Length > 0)
{
WebRequest wReq = WebRequest.Create(WSDL_URL);
httpReq = (HttpWebRequest)wReq;
httpReq.Timeout = 99999;
httpReq.ReadWriteTimeout = 99999;
httpReq.ServicePoint.MaxIdleTime = 99999;
httpReq.AllowWriteStreamBuffering = true;
XmlDocument xsldoc = new XmlDocument();
StringBuilder soapListReq = new StringBuilder();
soapListReq.Append("<?xml version='1.0' encoding='UTF-8'?> ");
soapListReq.Append("<soap:Envelope ");
soapListReq.Append(" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
soapListReq.Append("<soap:Body>");
soapListReq.Append("<GetCashFlow xmlns='TestProject.Services'>");
soapListReq.Append("<inputparam>");
soapListReq.Append("<ac_unq_ids xmlns='TestProject.Application.wsPMCData.Cashflow'>");
soapListReq.Append("<ac_unq_id>1234567890</ac_unq_id>");
soapListReq.Append("</ac_unq_ids>");
soapListReq.Append("<period_from xmlns='TestProject.Application.wsPMCData.Cashflow'>2008-05-01</period_from>");
soapListReq.Append("<period_to xmlns='TestProject.Application.wsPMCData.Cashflow'>2010-08-31</period_to>");
soapListReq.Append("<selected_curr xmlns='TestProject.Application.wsPMCData.Cashflow'>USD</selected_curr>");
soapListReq.Append("<report_type xmlns='TestProject.Application.wsPMCData.Cashflow'>I</report_type>");
soapListReq.Append("</inputparam>");
soapListReq.Append("</GetCashFlow>");
soapListReq.Append("</soap:Body>");
soapListReq.Append("</soap:Envelope>");
//Add Soap Header
httpReq.Headers.Add("SOAPAction", "http://tempuri.org/GetCashFlowDetail");
httpReq.Headers.Add("Version", "1.0");
httpReq.Headers.Add("OnBehalfOf", "");
httpReq.Headers.Add("Role", "1");
httpReq.Headers.Add("EndPoint", "WSHttpBinding_IwsPMCData");
//Add Http Headers
httpReq.Headers.Add("Version", "1.0");
httpReq.Headers.Add("OnBehalfOf", "");
httpReq.Headers.Add("Role", "1");
httpReq.Headers.Add("EndPoint", "WSHttpBinding_IwsPMCData");
httpReq.Headers.Add("ServiceId", "001");
httpReq.Headers.Add("DateTime", DateTime.Now.ToString("MM/dd/yyyy"));
httpReq.Headers.Add("ClientApplication", "WSHttpBinding_IwsPMCData");
httpReq.Headers.Add("TraceWebMethod", "false");
httpReq.Headers.Add("ClientTouchPoint", "ClientTouchPoint");
httpReq.Headers.Add("ChannelInfo", "ChannelInfo");
httpReq.Headers.Add("Environment", "WINOS");
httpReq.Headers["http_tmsamlsessionticket"] = string.Empty;
SMCookie = IFCookies["SMSESSION"];
httpReq.Headers.Add("MSMUSER", successfuluserid);
httpReq.Headers.Add("MSMSESSIONID", SMCookie.Value);
httpReq.Headers.Add("http_tmsamlsessionticket", "");
httpReq.Headers.Add("AcctGroupID", "");
httpReq.Headers.Add("AcctGroupType", "");
httpReq.Headers.Add("ExternalContext", "");
httpReq.Headers.Add("MSAML_ASSERTION", "");
string sSMCookie = "SMSESSION=" + SMCookie.Value;
httpReq.Headers["Cookie"] = sSMCookie;
httpReq.Method = "POST";
httpReq.ContentType = "text/xml; charset=utf-8";
httpReq.ContentLength = soapListReq.ToString().Length;
Stream StreamGoingtoWebservice;
StreamGoingtoWebservice = httpReq.GetRequestStream();
ASCIIEncoding ascEnc = new ASCIIEncoding();
Byte[] byteArray = ascEnc.GetBytes(soapListReq.ToString());
StreamGoingtoWebservice.Write(byteArray, 0, byteArray.Length);
StreamGoingtoWebservice.Close();
HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse();
if (httpRes.StatusCode == HttpStatusCode.OK)
{
StreamReader strmRdr = new StreamReader(httpRes.GetResponseStream());
//XmlReader xr = XmlReader.Create(strmRdr);
strCustomModels = strmRdr.ReadToEnd();
strmRdr.Close();
}
}
}
catch (Exception ex)
{
throw;
}
return strCustomModels;
}
公共静态字符串CustomSoapReq(字符串WSDL\u URL)
{
string strustommodels=string.Empty;
尝试
{
System.Net.Cookie SMCookie;
CookieCollection;
string successfuluserid=string.Empty;
CLSGFW实用工具clsutill;
HttpWebRequest-httpReq;
SMCookie=新Cookie();
IFCookies=新的CookieCollection();
clsutill=new ClsGFWUtility();//用于获取cookie信息的内置函数
IFCookies=clsutill.GetCookie(“LogonUrl”、“IFLoginUserId”、“IFLoginPassword”、out successfuluserid);
if(IFCookies!=null&&successfuluserid.Length>0)
{
WebRequest wReq=WebRequest.Create(WSDL\uurl);
httpReq=(HttpWebRequest)wReq;
httpReq.Timeout=99999;
httpReq.ReadWriteTimeout=99999;
httpReq.ServicePoint.MaxIdleTime=99999;
httpReq.AllowWriteStreamBuffering=true;
XmlDocument xsldoc=新的XmlDocument();
StringBuilder soapListReq=新的StringBuilder();
soapListReq.追加(“”);
soapListReq.追加(“”);
soapListReq.追加(“”);
soapListReq.追加(“”);
soapListReq.追加(“”);
soapListReq.追加(“”);
soapListReq.Append(“1234567890”);
soapListReq.追加(“”);
soapListReq.追加(“2008-05-01”);
soapListReq.追加(“2010-08-31”);
soapListReq.追加(“美元”);
soapListReq.追加(“I”);
soapListReq.追加(“”);
soapListReq.追加(“”);
soapListReq.追加(“”);
soapListReq.追加(“”);
//添加Soap头
httpReq.Headers.Add(“SOAPAction”http://tempuri.org/GetCashFlowDetail");
添加(“版本”,“1.0”);
httpReq.Headers.Add(“OnBehalfOf”,“OnBehalfOf”);
添加(“角色”,“1”);
添加(“端点”,“WSHttpBinding_iwspmdata”);
//添加Http头
添加(“版本”,“1.0”);
httpReq.Headers.Add(“OnBehalfOf”,“OnBehalfOf”);
添加(“角色”,“1”);
添加(“端点”,“WSHttpBinding_iwspmdata”);
httpReq.Headers.Add(“ServiceId”,“001”);
httpReq.Headers.Add(“DateTime”,DateTime.Now.ToString(“MM/dd/yyyy”);
添加(“客户端应用程序”、“WSHttpBinding_iwspmdata”);
httpReq.Headers.Add(“TraceWebMethod”、“false”);
添加(“ClientTouchPoint”、“ClientTouchPoint”);
httpReq.Headers.Add(“ChannelInfo”、“ChannelInfo”);
添加(“环境”、“WINOS”);
httpReq.Headers[“http_tmsamlsessionticket”]=string.Empty;
SMCookie=IFCookies[“SMSESSION”];
httpReq.Headers.Add(“MSMUSER”,successfuluserid);
添加(“MSMSESSIONID”,SMCookie.Value);
Add(“http_tmsamlsessionticket”,”);
Add(“AcctGroupID”,“AcctGroupID”);
Add(“AcctGroupType”,“AcctGroupType”);
Add(“ExternalContext”,“ExternalContext”);
Add(“MSAML_断言”,即“);
字符串sSMCookie=“SMSESSION=“+SMCookie.Value;
httpReq.Headers[“Cookie”]=sSMCookie;
httpReq.Method=“POST”;
httpReq.ContentType=“text/xml;charset=utf-8”;
httpReq.ContentLength=soapListReq.ToString().Length;
河道通航服务;
StreamGoingTowerService=httpReq.GetRequestStream();
ascienceoding ascEnc=新的ascienceoding();
Byte[]byteArray=ascEnc.GetBytes(soapListReq.ToString());
streamgoingtowerservice.Write(byteArray,0,byteArray.Length);
StreamGoingTowerService.Close();
HttpWebResponse httpRes=(HttpWebResponse)httpReq.GetResponse();
if(httpRes.StatusCode==HttpStatusCode.OK)
{
StreamReader strmRdr=新的StreamReader(httpRes.GetResponseStream());
//XmlReader xr=XmlReader.Create(strmRdr);
strcstomodels=strmRdr.ReadToEnd();
strmRdr.Close();
}
}
}
捕获(例外情况除外)
{
投掷;
}
返回strucstommodels;
}
我们为ASMX服务做了类似的事情。但不确定这是否也适用于WCF。尽管如此,我尝试运行这段代码,但在第HttpWebResponse httpRes=(HttpWebResponse)httpReq.GetResponse()行得到500个服务器错误(不确定是否与错误的头信息有关)
我不太确定我是否能够正确地描述我的问题。如果您需要更多信息,请告诉我。首先,您要添加的是HTTP头,而不是SOAP头。其次,这些不是标准的标题,所以我不确定即使对于ASMX,您是否正确添加了它们。请显示您用于WCF的代码。john,我已经添加了代码中我们尝试构建自定义soap请求的部分。