aa13551411 2023-02-06 16:23 采纳率: 0%
浏览 84
已结题

c#调用soap出错,soapui中可以

用调用一个SOAP接口,SOAPUI可以获取到,但在c#里实现出错

img

C#代码如下



            string reqstr = "";
//reqstr += " < soapenv:Envelope xmlns:soapenv =\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tip=\"http://www.dsc.com.tw/tiptop/TIPTOPServiceGateWay\">";
//            reqstr += "   <soapenv:Header/>";
//            reqstr += "   <soapenv:Body>";
//            reqstr += "      <tip:GetWOData2Request>";
//            reqstr += "         <tip:request>";
            reqstr += "<Request> ";
            reqstr += "  <Access>";
            reqstr += "    <Authentication user=\"tiptop\" password=\"tiptop\"/>  ";
            reqstr += "    <Connection application=\"MES\" source=\"192.168.1.1\"/>  ";
            reqstr += "    <Organization name=\"TESTSM\"/>  ";
            reqstr += "    <Locale language=\"zh_tw\"/>  ";
            reqstr += "  </Access>  ";
            reqstr += "  <RequestContent> ";
            reqstr += "    <Parameter> ";
            reqstr += "      <Record> ";
            reqstr += "       <Field name=\"sfb01\" value=\"\"/>  ";
            reqstr += "        <Field name=\"sfb05\" value=\"\"/>  ";
            reqstr += "        <Field name=\"sfb81\" value=\"\"/>  ";
            reqstr += "        <Field name=\"sfb04\" value=\"\"/>  ";
            reqstr += "      </Record> ";
            reqstr += "    </Parameter> ";
            reqstr += "  </RequestContent> ";
            reqstr += "</Request>";
            //reqstr += "         </tip:request>";
            //reqstr += "      </tip:GetWOData2Request>";
            //reqstr += "   </soapenv:Body>";
            //reqstr += "</soapenv:Envelope>";



            string _url = "http://192.168.10.26/web/ws/r/aws_ttsrv2_toptest/";
            Uri uri = new Uri(_url);
            var request = (HttpWebRequest)WebRequest.Create(_url);
            //WebRequest request = WebRequest.Create(uri);
            request.Method = "POST";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.ContentType = "text/xml;charset=UTF-8";// "application/xml";
            //request.ContentType = "application/x-www-form-urlencoded";
            //request.Headers.Add("Accept-Encoding", "gzip,deflate");
            request.ProtocolVersion = HttpVersion.Version10;
            //request.Timeout = 14000;
            
     
            byte[] byteData = Encoding.UTF8.GetBytes(reqstr);
            int length = byteData.Length;

            request.ContentLength = length;
            //request.ContentType = "gzip";
           
            System.IO.Stream writer = request.GetRequestStream();
            
            writer.Write(byteData, 0, length);
            writer.Close();
            var response = (HttpWebResponse)request.GetResponse();

            string rp = response.ToString();
运行结果

远程服务器返回错误: (415) Unsupported Media Type。

首先我不确定reqstr是不是对的,另外不确定是不是ContentType 不正确导致的,我换了几种写法,跟soapui设置保持一致也是同样的报错
  • 写回答

3条回答 默认 最新

  • Oops_GTC 2023-02-06 16:41
    关注

    该代码片段缺少了处理响应数据的代码。你需要使用 StreamReader 从 response 对象读取数据,例如:

    
    var response = (HttpWebResponse)request.GetResponse();
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        string result = reader.ReadToEnd();
        Console.WriteLine(result);
    }
    
    

    服务器拒绝了你的请求,因为你提交的数据格式不支持。
    你可以尝试在请求的头部添加下面的代码,以确保请求的数据格式正确:

    request.Headers.Add("SOAPAction", "\"http://tempuri.org/GetWOData2Request\"");
    
    
    

    返回错误代码 415 表示不支持的媒体类型。这可能是因为服务器无法识别您请求的内容类型,因此不能处理请求。
    请确保在请求中设置的内容类型与接口期望的一致。在您的代码中,您设置了 ContentType = "text/xml;charset=UTF-8",请确保此内容类型是接口支持的,如果不支持,请使用接口所支持的正确的内容类型。

    评论

报告相同问题?

问题事件

  • 系统已结题 2月14日
  • 创建了问题 2月6日