简单的 Restclient POST 问题

发布于 2024-12-26 11:45:23 字数 947 浏览 0 评论 0原文

花了几个小时寻找答案,但似乎找不到任何东西。我正在尝试使用 Rest 客户端从 Windows 窗体项目更新到 Web 服务。我已成功地“GET”到 Datagridview,但在使用“POST”时遇到问题。这是我尝试使用的代码片段(生成许可证是一种将其写入 xml 文档并返回流的方法):

编辑: HttpResponse 给我这个错误:

<?xml version="1.0"?>
<Licenses_table><Licenses><ErrorCode>HY000</ErrorCode><DriverCode>1364</DriverCode><Message>Field 'Code' doesn't have a default value</Message></Licenses></Licenses_table>

string Url = "http://localhost:8810/ReplicationService.php/Licenses/";

byte[] dataByte = GenerateLicense(Code, Version1, Name);

HttpWebRequest POSTrequest = (HttpWebRequest)WebRequest.Create(Url);

POSTrequest.Method = "POST";

POSTrequest.ContentType = "text/xml";
POSTrequest.KeepAlive = false;
POSTrequest.Timeout = 5000;

POSTrequest.ContentLength = dataByte.Length;

Stream POSTstream = POSTrequest.GetRequestStream();

POSTstream.Write(dataByte, 0, dataByte.Length);

Been searching for hours for an answer and cant seem to find anything. I am trying to update from a Windows forms project to a Web Service using Rest client. I have successfully managed to "GET" into a Datagridview, but i am having trouble using "POST". Here is a snippet of the code i am trying to use (Generate License is a method that writes it to a xml document and returns the stream):

EDIT: The HttpResponse is giving me this error:

<?xml version="1.0"?>
<Licenses_table><Licenses><ErrorCode>HY000</ErrorCode><DriverCode>1364</DriverCode><Message>Field 'Code' doesn't have a default value</Message></Licenses></Licenses_table>

string Url = "http://localhost:8810/ReplicationService.php/Licenses/";

byte[] dataByte = GenerateLicense(Code, Version1, Name);

HttpWebRequest POSTrequest = (HttpWebRequest)WebRequest.Create(Url);

POSTrequest.Method = "POST";

POSTrequest.ContentType = "text/xml";
POSTrequest.KeepAlive = false;
POSTrequest.Timeout = 5000;

POSTrequest.ContentLength = dataByte.Length;

Stream POSTstream = POSTrequest.GetRequestStream();

POSTstream.Write(dataByte, 0, dataByte.Length);

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

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

发布评论

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

评论(1

撑一把青伞 2025-01-02 11:45:23

这是您展示的全部代码吗?我不明白您在哪里关闭请求流,然后在 WebRequest 上调用 GetResponse() ?

Is that the whole code you are showing? I dont see where you are closing the request stream, and then calling GetResponse() on the WebRequest?

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