如何让HTML POST数据并传递给web服务? [英] how get html post data and pass to a webservice?

查看:1649
本文介绍了如何让HTML POST数据并传递给web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用WCF的一些知识,但我不知道如何构建它,并能够从HTML-POST接收数据结果
里面我的示例HTML是这样的:

 < HTML和GT; 
<身体GT;
<形式方法=POSTACTION =#>
名称:LT;输入类型=文本ID =改为txtNameVALUE =/>< BR />
年龄:其中;输入类型=文本ID =txtAgeVALUE =/>< BR />
地址:<输入类型=文本ID =txtAddressVALUE =/>< BR />
<输入类型=提交值=发送/>
< /表及GT;
< / BODY
< / HTML>

和我的WCF里面是:



 命名空间WCF 
{
[的ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall) ]
公共类用户
{
WebInvoke(UriTemplate =,方法=POST)]
公共DataFromHTMLPost(的UserData实例)
{
抛出新NotImplementedException();
}
}
}



任何人都可以给一些建议上我怎么可能通过从HTML-POST数据到web服务?结果
或给我一些链接作为参考。非常感谢!


解决方案

您将需要后面抓住从代码中的数据,然后通过它传递到Web服务。



您可以=服务器添加到其中每个输入字段,并直接前往



<$获取数据p $ p> txtName.Text

或你可以得到所有已发布数据通过

 的NameValueCollection雷士=的Request.Form; 

字符串名称=雷士照明[改为txtName];



然后,只需调用Web服务,我会假设你已经添加为项目中的服务引用?



UPDATE(自学习它的一个PHP客户端访问.NET WCF)



为了得到数据后做到这一点:

  $项目= $ _ POST ['项目']; 



产品输入字段的名称。



PHP代码连接到WCF(使用JSON)

  / ** 
* @参数$服务名JSON服务来调用
* @返回字符串的URL字符串
* /
功能request_Url($服务)
{

返回的http:/ /api.domain.com/api.svc'.'/'.$service

}

/ **
*
* @返回阵列|虚假竞赛列表或失败
假* /
功能get_contestList(){

$ jsonStr = json_decode(的file_get_contents($这个 - > request_Url(方法名) ),TRUE);

$回报jsonStr;
}

在WCF,你需要改变你的方法签名



  [OperationContract的] 
[WebGet(ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
UriTemplate =/方法名)]
公共字符串方法名()
{

}


I have some knowledge on using WCF, but I don't know how to construct it and be able to receive data from HTML-POST.
Inside my sample Html is this:

<html>
<body>
    <form method="POST" action="#">
        Name:   <input type="text" id="txtName" value="" /><br />
        Age:    <input type="text" id="txtAge" value="" /><br />
        Address:<input type="text" id="txtAddress" value="" /><br />
        <input type="submit" value="Send" />
    </form>
</body
</html>  

And inside my WCF is:

namespace WCF
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class User
    {
        WebInvoke(UriTemplate = "", Method = "POST")]
        public DataFromHTMLPost(UserData instance)
        {
            throw new NotImplementedException();
        }
    }
}

Can anyone give some suggestion on how could I pass the data from HTML-POST to webservice?
Or give me some links as reference. Thanks a lot!

解决方案

You will need to grab the data from code behind and then pass it through to the web service.

You can either add runat="server" to each of those input fields and get the data directly by going

txtName.Text

or you can get all the posted data by

NameValueCollection nvc = Request.Form; 

String name = nvc["txtName"];

Then just call the web service which I would assume you have added as a Service Reference in your project?

UPDATE (Since learning its a PHP client accessing .NET WCF)

For getting post data do this:

$item = $_POST['item'];

item is the name of the input field.

PHP code to connect to WCF (using JSON)

  /**
    * @param $service name of JSON service to call
    * @return string url in string
    */
  function request_Url( $service )
{

return 'http://api.domain.com/api.svc'.'/'.$service

}

/**
 * 
 * @return array|false Contest List or false on failure
 */
function get_contestList() {

    $jsonStr = json_decode(file_get_contents($this->request_Url('MethodName')), TRUE);

    return $jsonStr;
}

In the WCF you will need to change your method signatures to

[OperationContract]    
[WebGet(ResponseFormat = WebMessageFormat.Json, 
        RequestFormat = WebMessageFormat.Json,               
        UriTemplate = "/MethodName")]
public String MethodName()
{

}

这篇关于如何让HTML POST数据并传递给web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆