0
我想在C#控制台应用程序中发布请求,并且出现此错误。该请求在SOAPUI中工作,没有WSDL文件,因此我必须直接在代码中加载XML(我高度怀疑这是错误的来源)。在C#控制台应用程序中发送SOAP请求时发生错误
这里是我使用的这个SOAP请求传送的C#控制台应用程序。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace IMSPostpaidtoPrepaid
{
class Program
{
static void Main(string[] args)
{
Program obj = new Program();
obj.createSOAPWebRequest();
}
//Create the method that returns the HttpWebRequest
public void createSOAPWebRequest()
{
//Making the Web Request
HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://127.1.1.0:9090/spg");
//Content Type
Req.ContentType = "text/xml;charset=utf-8";
Req.Accept = "text/xml";
//HTTP Method
Req.Method = "POST";
//SOAP Action
Req.Headers.Add("SOAPAction", "\"SOAPAction:urn#LstSbr\"");
NetworkCredential creds = new NetworkCredential("username", "pass");
Req.Credentials = creds;
Req.Headers.Add("MessageID", "1"); //Adding the MessageID in the header
string DN = "+26342720450";
string DOMAIN = "ims.telone.co.zw";
//Build the XML
string xmlRequest = String.Concat(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>",
"<soap:Envelope",
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
" xmlns:m=\"http://www.huawei.com/SPG\"",
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"",
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">",
" <soap:Body>",
"<m:LstSbr xmlns=\"urn#LstSbr\">",
" <m:DN>", DN, "</ m:DN>",
" <m:DOMAIN>", DOMAIN, "</ m:DOMAIN>",
" </m:LstSbr>",
" </soap:Body>",
"</soap:Envelope>");
//Pull Request into UTF-8 Byte Array
byte[] reqBytes = new UTF8Encoding().GetBytes(xmlRequest);
//Set the content length
Req.ContentLength = reqBytes.Length;
//Write the XML to Request Stream
try
{
using (Stream reqStream = Req.GetRequestStream())
{
reqStream.Write(reqBytes, 0, reqBytes.Length);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception of type " + ex.GetType().Name + " " + ex.Message);
Console.ReadLine();
throw;
}
//Headers and Content are set, lets call the service
HttpWebResponse resp = (HttpWebResponse)Req.GetResponse();
string xmlResponse = null;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
xmlResponse = sr.ReadToEnd();
}
Console.WriteLine(xmlResponse);
Console.ReadLine();
}
}
}
看看吧 – nktsamba
嘿@nktsamba,你有机会解决它吗? –