Javascript WCF Restfull WS-post方法和HTML表单
我在IIS上发布了简单的.net restful web服务:Javascript WCF Restfull WS-post方法和HTML表单,javascript,html,.net,wcf,wcf-rest,Javascript,Html,.net,Wcf,Wcf Rest,我在IIS上发布了简单的.net restful web服务: [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "formTestGET?firstInput={firstInput}&socondInput={socondInput}")] string formTestGET(string firstInpu
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "formTestGET?firstInput={firstInput}&socondInput={socondInput}")]
string formTestGET(string firstInput, string socondInput);
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "formTestPOST")]
string formTestPOST(string testInput);
方法的实施:
public string formTestGET(string firstInput, string socondInput)
{
try
{
return "First Input value: " + firstInput + " Second Input value: " + socondInput;
}
catch (Exception e)
{
return e.Message;
}
}
public string formTestPOST(string testInput)
{
try
{
return "Post paramether value: " + testInput;
}
catch (Exception e)
{
return e.Message;
}
}
我的html表单:
<form method="post" action="http://localhost/HTML5RestfulService/Service1.svc/formTestPOST">
<fieldset>
<legend>Form Post Request</legend>
<input name="testInput"/>
<button>Make Post Request</button>
</fieldset>
</form>
表格邮寄申请
发帖
我只想通过html表单使用此服务。我的POST方法有问题。当我用Ajax(来自java脚本)调用它时,它工作得很好,但是通过表单我无法得到响应。
我收到“400错误请求”作为错误
当我想通过表单调用WS时,是否应该对其进行不同的配置
请给我任何建议。
如果您想从基本表单调用WCF服务,而不使用ECMAScript(JavaScript)、Silverlight或Flash,那么我认为您应该遵循Cheng先生的建议,将formTestPOST操作的输入参数更改为单个流参数
有关详细信息,请参阅以下代码:
//文件:IService1.cs
//(根据米洛斯、肖恩·艾利、史蒂文·程和
//网络社区)
//
//参考资料
//[1]-Cheng,Steven(MSFT);比尔2010
//我将如何使用WCF REST完成以下表单发布?
//MSDN论坛:2010年8月12日
// http://social.msdn.microsoft.com/Forums/vstudio/en-US/dde73561-29c0-4c34-b18a-990ae114b92c/how-would-i-do-the-following-form-post-with-wcf-rest
//[引用日期:2013年9月27日]
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.IO;
[服务合同]
公共接口IService1
{
//要将一个基本的HTML表单发布到WCF操作,我想是程先生
//微软建议用户将WCF放入“原始数据传输”中
//通过将所有输入参数替换为单个流[1]
[经营合同]
[WebInvoke(Method=“POST”,ResponseFormat=WebMessageFormat.Json,UriTemplate=“formTestPOST”)]
字符串formTestPOST(流测试输入);
}
及
//文件:Service1.cs
//(基于以下人员的工作:米洛斯;肖恩·伊雷;所有列出的人)
//在下面的参考资料部分;以及网络社区)
//
//参考资料
//[1]-Brian;编码器;科普,达伦;詹姆斯;用户1749204
//如何从MemoryStream中获取字符串?
//堆栈溢出:2008年9月29日
// http://stackoverflow.com/questions/78181/how-do-you-get-a-string-from-a-memorystream
//[引用日期:2013年9月27日]
//
//[2]-阿特拉斯,迈克;讨价还价;格雷威尔,马克
//在.NET/C中解析“多部分/表单数据”#
//堆栈溢出:2009年11月11日
// http://stackoverflow.com/questions/1716868/parsing-multipart-form-data-in-net-c
//[引用日期:2013年9月27日]
//
//[3]-antscode
//多部分表单数据解析器
//CodePlex:2009-2012
// http://multipartparser.codeplex.com/SourceControl/list/changesets
//[引用日期:2013年9月27日]
//
//[4]-Polidori,Lorenzo;教育科学系。;伍兹,杰克
//将文件从Flex上载到WCF REST流问题(如何在REST WS中解码多部分表单post)
//堆栈溢出:2012年1月20日
// http://stackoverflow.com/questions/5423031/uploading-file-from-flex-to-wcf-rest-stream-issues-how-to-decode-multipart-form
//[引用日期:2013年9月27日]
使用制度;
使用System.IO;
使用System.Web;
公共类服务1:IService1
{
公共字符串formTestPOST(流测试输入)
{
//从流testInput中获取post数据
//既然您的表单成功地启动了此操作,您
//可以使用[1]中提到的Brian技术转换
//串流
StreamReader someReader=新的StreamReader(测试输入);
String theInput=someReader.ReadToEnd();
//不幸的是,互联网上的各个地方似乎都在
//指示字符串中现在包含的数据
//输入是多部分表单数据。如果您愿意
//要在界面中使用ASP.NET兼容模式,
//那么你就可以使用
//Mike Atlas和Mark Gra砾在[2]中,但让我们假设
//您不能使用兼容模式。那么您可以
//尝试使用antscode编写的多部分解析器
//在[3]或者你可以做我做的事情,猜你的答案
//因为你有一个简单的表单,你可以
//只需通过替换来猜测解析的方式
//“testInput”一无所有,只剩下一部分。
//这在更复杂的表单上不起作用,但在
//这个例子,你会得到更多的回击
//以及为什么在[4]中需要UrlDecode
输入=输入。替换(“testInput=”,“”);
theInput=HttpUtility.UrlDecode(theInput);
返回“Post参数值:”+输入;
}
}