我在SOAP UI测试通过了SAP的SOAP接口,在C#调用,一直提示“服务器内部错误 500”。经过分析对比发现
在SOAPUI中的WA中使用了Randomly generate MessageID这个选项,我在c#如何增加这个选项
public static string TestSAPService()
{
//读取xml格式文件
string soapFile = "d:\\journal.txt";
StringBuilder soap = new StringBuilder();
soap.Clear();
try
{
FileStream fs = new FileStream(soapFile, FileMode.Open, FileAccess.Read);
if (fs.CanRead)
{
// 读取时加入编码信息,否则读取汉字会乱码
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
string strline = sr.ReadLine();
while (strline != null )
{
strline = sr.ReadLine();
soap = soap.Append(strline);
}
sr.Close();
}
}
catch (Exception ex)
{ throw ex; }
//发起请求
Uri url = new Uri("https://my300197.saps4hanacloud.cn/sap/bc/srt/scs_ext/sap/journalentrybulkcreationreques");
string a = "";
//try
//{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;//设置SSL/TLS 安全通道协议
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); //定义Request对象
webRequest.ProtocolVersion = HttpVersion.Version11;
webRequest.Timeout = 10000;
webRequest.Headers.Add("Username:FI_USER");
webRequest.Headers.Add("Password:ryhVmAgovweYfS}MvqKdjJqccVqGMpjBexromw6Y");
webRequest.Headers.Add("Authorization:Basic RklfVVNFUjpyeWhWbUFnb3Z3ZVlmU31NdnFLZGpKcWNjVnFHTXBqQmV4cm9tdzZZ");
webRequest.ContentType = "text/xml; charset=UTF-8";
//webRequest.Accept = @"application/xml";//获取XML格式
webRequest.Method = "POST";
webRequest.Headers.Add("soapAction", "http://sap.com/xi/SAPSCORE/SFIN/JournalEntryBulkCreationRequest_In/JournalEntryBulkCreationRequest_InRequest");
byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
//requestStream.Write(paramBytes, 0, paramBytes.Length);
webRequest.ContentLength = paramBytes.Length;
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(paramBytes, 0, paramBytes.Length);//写入参数
requestStream.Close();
//响应
WebResponse webResponse = webRequest.GetResponse();
using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(myStreamReader.ReadToEnd());
a += myStreamReader.ReadToEnd();
}
//}
//catch (Exception err)
//{
// //MessageBox(err.Message);
// return err.ToString();
//}
return a;
}