html webservice数据交互_如何让HTML POST数据并传递给web服务?

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:

Name:

Age:

Address:

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()

{

}

/* 复杂邮件程序完整Java源码,支持添加附件,图片,HTML格式文本,支持远程WebService调用*/ package com.hx.mail; import java.io.File; import java.util.Map; import javax.mail.Message.RecipientType; /** * MailServices 邮件接收发送接口定义类 * * @author 380595305@qq.com * Date 2010-05-11 * @version 1.0 */ public interface HexiangMailService { /** * sendMail 发送邮件函数 * * @param sender 是String类型,邮件发送者信息 * @param password 是String类型,邮件发送者密码 * @param addressee 是String类型,邮件接收者信息 * @param subject 是String类型,传入邮件主题 * @param text 是String类型,传入邮件消息 */ void sendMail(String sender,String password,String addressee,String subject,String text) throws Exception; /** * sendMail 发送邮件函数 * * @param sender 是String类型,邮件发送者信息 * @param password 是String类型,邮件发送者密码 * @param addressee 是String类型,邮件接收者信息 * @param subject 是String类型,传入邮件主题 * @param text 是String类型,传入邮件消息 * @param enclosures Map 邮件附件 * @param copyToSends Map 邮件抄送信息 */ void sendMail(String sender,String password,String addressee,String subject,String text,Map enclosures,Map copyToSends) throws Exception; /** * sendMail 发送邮件函数 * * @param sender 是String类型,邮件发送者信息 * @param password 是String类型,邮件发送者密码 * @param subject 是String类型,传入邮件主题 * @param imgs 是File[]类型,邮件正文中附件的图片信息 * @param htmlContent 是String类型,传入邮件消息正文 * @param enclosures Map 邮件附件 * @param copyToSends Map 邮件抄送信息 */ void sendMail(String sender,String password,String subject,File[] imgs,String htmlContent,Map enclosures,Map copyToSends) throws Exception; } package com.hx.mail; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Proper
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值