[英]HTTP POST to a WCF service
I'm trying to invoke a WCF service through HTTP POST, but the service returns a 400 error. 我正在尝试通过HTTP POST调用WCF服务,但该服务返回400错误。 I don't know whether this is due to the OperationContract or the way I'm doing the POST.
我不知道这是由于OperationContract还是我正在进行POST的方式。 This is what the contract looks like on the server-side:
这就是合同在服务器端的样子:
[OperationContract, WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]
Stream Download(string username, int fileid);
And here's how I'm trying to invoke the service through a test console app: 以下是我试图通过测试控制台应用程序调用服务的方法:
HttpWebRequest webRequest = WebRequest.Create("http://localhost:8000/File/Download") as
HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes("username=test&fileid=1");
Stream os = null;
webRequest.ContentLength = bytes.Length;
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();
WebResponse webResponse = webRequest.GetResponse();
edit: I should make it clear that my goal is to test the service, and not to make it accept raw HTTP POSTs. 编辑:我应该说清楚我的目标是测试服务,而不是让它接受原始的HTTP POST。 If there's a better way I can test the service, please feel free to share.
如果有更好的方法可以测试服务,请随时分享。
This is pretty simple process, but not readily accessible or straight-forward (as is unfortunately the case with a many aspects of WCF) Please check out this post for clarification: 这是一个非常简单的过程,但不容易访问或直接(不幸的是WCF的许多方面的情况)请查看这篇文章以获得澄清:
service contract: 服务合同:
[ServiceContract]
public interface ISampleService
{
[OperationContract]
[WebInvoke(UriTemplate = "invoke")]
void DoWork(Stream input);
}
HTML source: HTML源代码:
<form method="post" action="Service.svc/invoke">
<label for="firstName">First Name</label>: <input type="text" name="firstName" value="" />
<br /><br />
<label for="lastName">Last Name</label>: <input type="text" name="lastName" value="" />
<p><input type="submit" /></p>
</form>
Code Behind: 代码背后:
public void DoWork(Stream input)
{
StreamReader sr = new StreamReader(input);
string s = sr.ReadToEnd();
sr.Dispose();
NameValueCollection qs = HttpUtility.ParseQueryString(s);
string firstName = qs["firstName"];
string lastName = qs["lastName"];
}
If you are able to use other content types, you could instead use json, which will work in your example. 如果您能够使用其他内容类型,则可以改为使用json,它将在您的示例中使用。
Change 更改
webRequest.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes("username=test&fileid=1");
to 至
webRequest.ContentType = "application/json";
byte[] bytes = Encoding.ASCII.GetBytes("{\"username\":\"test\",\"fileid\":1");
If you must use the application/x-www-form-urlencoded content type, google wcf application/x-www-form-urlencoded for several posts and other SO questions describing workarounds. 如果您必须使用application / x-www-form-urlencoded内容类型,请google wcf application / x-www-form-urlencoded以获取几个帖子以及描述解决方法的其他SO问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.