wcf通过httpwebrequest发布请求 [英] wcf post request through httpwebrequest
本文介绍了wcf通过httpwebrequest发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/chkUserPwd", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
void CheckUserAndPassword(string user, string pwd);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public void CheckUserAndPassword(string user, string pwd)
{
if (user == "me" && pwd == "123")
{
//return true;
HttpContext.Current.Response.Write("Welcome");
}
else
{
//return false;
HttpContext.Current.Response.Write("Invalid");
}
}
}
--Web.Config--
--Web.Config--
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="BehaveWebHttp">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="BizConnectPro_Webservice.Service1">
<endpoint behaviorConfiguration="BehaveWebHttp" binding="webHttpBinding"
bindingConfiguration="" contract="BizConnectPro_Webservice.IService1" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
我是使用下面的代码通过HttpWebRequest使用此Web服务和
得到错误远程服务器返回错误:(400)错误请求。它适用于get方法.----
I am using below code to use this web service through HttpWebRequest and
got error "The remote server returned an error: (400) Bad Request." It working fine for get method.----
string postData = string.Format("user={0}&pwd={1}", "me", "123");
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:52631/Service1.svc/chkUserPwd");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
try
{
using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter2.Write(postData);
}
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
// dumps the HTML from the response into a string variable
postData = responseReader.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
推荐答案
credential obj = new credential();
obj.user = "me";
obj.pwd = "123";
JavaScriptSerializer js = new JavaScriptSerializer();
string postData= js.Serialize(obj);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://localhost:52631/Service1.svc/chkUserPwd");
webRequest.Method = "POST";
webRequest.ContentType = "application/json"
webRequest.ContentLength = postData.Length;
try
{
using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter2.Write(postData);
}
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
// dumps the HTML from the response into a string variable
postData = responseReader.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
public class credential
{
public string user { get; set; }
public string pwd { get; set; }
}
正如我在WebInvoke上提到的那样是BodyStyle = WebMessageBodyStyle.Wrapped然后我们必须发布数据json或xml。
As I mentioned on WebInvoke is BodyStyle = WebMessageBodyStyle.Wrapped then we must post the data json or xml.
这篇关于wcf通过httpwebrequest发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文