简单的 Restclient POST 问题
花了几个小时寻找答案,但似乎找不到任何东西。我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您展示的全部代码吗?我不明白您在哪里关闭请求流,然后在 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?