Web 服务 XML 请求中的错误处理

发布于 2024-12-01 20:40:06 字数 892 浏览 0 评论 0原文

我一直在尝试执行 XML 请求。我遇到了很多我设法解决的问题。但这个问题我无法解决。 这是脚本:

$url ="WebServiceUrl";
$xml="XmlRequest";
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_MUTE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
 echo $output;

它给了我这个错误:

System.InvalidOperationException:请求格式无效:text/xml。在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

我对此仍然是菜鸟。所以对我宽松点:) 谢谢。

I've been trying to perform an XML request. I've faced so many problems that I managed to solve. But this one I couldn't solve.
this is the script:

$url ="WebServiceUrl";
$xml="XmlRequest";
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_MUTE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
 echo $output;

It is giving me this error:

System.InvalidOperationException: Request format is invalid: text/xml. at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

I'm still a noob at this. So go easy on me:)
thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-12-08 20:40:06

看起来您正在以 text/xml 的形式发送内容,这不是它想要的。查找此 Web 服务的文档(例如 WSDL 内容)(如果有),并找出它接受的数据格式。

例如,请确保它并不是真正说在收到作为标准 HTML POST 变量的请求后将以 XML 形式进行响应。

Looks like you're sending stuff as text/xml, which is not what it wants. Find the docs for this web service e.g. WSDL stuff if it's there, and find out what data formats it accepts.

Be sure e.g. that it's not really saying it will respond in XML, after receiving a request as standard HTML POST variables.

辞慾 2024-12-08 20:40:06

HTTP POST 方法使用两种主要内容类型:application/x-www-form-urlencoded 和 multipart/form-data。

内容类型决定了 CURLOPT_POSTFIELDS 的格式。如果您使用默认值“application/x-www-form-urlencoded”,您可能需要使用 build_http_query() 来构造 url 编码的查询字符串。

如果您要发送非 ASCII 数据,您可以传递一个关联数组,其中包含与字段名称匹配的键和与该字段的值相对应的值。使用此技术将导致以 multipart/formdata 内容类型发出请求。

此时,听起来您的下一步应该是弄清楚 API 需要哪些字段。

application/x-www-form-urlencoded 或 multipart/form-数据?

There are two main content types used with the HTTP POST method: application/x-www-form-urlencoded and multipart/form-data.

The content-type determines what the format of the CURLOPT_POSTFIELDS should be. If you are using the default, which is "application/x-www-form-urlencoded" you probably want to use build_http_query() to construct the url encoded query string.

If you are sending non-ASCII data you canpass an associative array with keys that match the field names and values that correspond to the value for the field. Using this technique will cause the request to be issued with a multipart/formdata content-type.

At this point, it sounds like your next step should be figuring out what fields the API is expecting.

application/x-www-form-urlencoded or multipart/form-data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文