简体   繁体   English

.NET Web服务接收HTTP POST请求(500)内部服务器错误

[英].NET Web Service receive HTTP POST request (500) Internal Server Error

I am currently writing a C# web service which has several methods, one of which has to receive HTTP POST requests. 我目前正在编写一个C#Web服务,它有几种方法,其中一种方法必须接收HTTP POST请求。 The first thing i have done is alter the web.config file in the web service project as below. 我做的第一件事是改变web服务项目中的web.config文件,如下所示。

<webServices>
  <protocols>
    <add name="HttpSoap"/>
    <add name="HttpPost"/>
    <add name="HttpPostLocalhost"/>
    <add name="Documentation"/>
  </protocols>
</webServices>

I can run the web service locally and when i click on the method in the browser, i can see it handles HTTP POST requests and accepts args=string, as my signature of the web method accepts one string parameter named args. 我可以在本地运行Web服务,当我在浏览器中单击该方法时,我可以看到它处理HTTP POST请求并接受args = string,因为我的Web方法签名接受一个名为args的字符串参数。 I am then testing this via a test ASP.NET app using the code below to fire the HTTP POST request. 然后我通过测试ASP.NET应用程序使用下面的代码测试它来触发HTTP POST请求。

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ConfigurationManager.AppSettings["PaymentHubURL"].ToString());

request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

StringBuilder sb = new StringBuilder();
sb.Append("message_type=");
sb.Append(HttpUtility.UrlEncode("Txn_Response"));

byte[] bytes = UTF8Encoding.UTF8.GetBytes(sb.ToString());
request.ContentLength = bytes.Length;
using (Stream postStream = request.GetRequestStream())
{
    postStream.Write(bytes, 0, bytes.Length);
}

string test;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
     StreamReader reader = new StreamReader(response.GetResponseStream());

     test = reader.ReadToEnd();
}

But when i run this i get "The remote server returned an error: (500) Internal Server Error". 但是当我运行这个时,我得到“远程服务器返回错误:(500)内部服务器错误”。 If i remove the parameter, by removing the stringbuilder and byte code, as well as having no parameter in the web service, it works. 如果我删除参数,通过删除stringbuilder和字节代码,以及在Web服务中没有参数,它的工作原理。 So it is obviously a problem with the parameters. 所以这显然是参数的问题。 I actually want to send more data, and was using a string[] parameter in the web service, but this also failed. 我实际上想要发送更多数据,并在Web服务中使用string []参数,但这也失败了。

Can anyone help?? 谁能帮忙?

I would suggest that you reconsider your approach. 我建议你重新考虑你的方法。 Microsoft has written pretty awesome libraries for consuming web services, but there are two ways to do it - "add web reference" and "add service reference". 微软已经编写了非常棒的用于使用Web服务的库,但有两种方法可以实现 - “添加Web引用”和“添加服务引用”。

In your case, it seems you have an "asmx web service" so I would recommend that you add a "web reference" to you project in visual studio. 在您的情况下,似乎您有一个“asmx Web服务”,所以我建议您在visual studio中为您的项目添加“Web引用”。 This is assuming you are using visual studio. 这假设您正在使用visual studio。

After you add this web reference, you can create your client by "new"-ing it. 添加此Web引用后,您可以通过“new”创建客户端。 You can the execute any web method on this client. 您可以在此客户端上执行任何Web方法。 This is the easiest way to consume web services. 这是使用Web服务的最简单方法。 You do not have to deal with any http complications. 您不必处理任何http并发症。

Hope this helps. 希望这可以帮助。

I can guess you are building the HttpPost request wrongly. 我猜你是在错误地构建HttpPost请求。

Try to use the code showed at the link below to create your request: 尝试使用以下链接中显示的代码来创建您的请求:

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

Or probabily the response doesn't contain unicode char value 或者可能响应不包含unicode char值

try to copy and paste this code to get the response 尝试复制并粘贴此代码以获取响应

 System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    StreamReader objSR;

webResponse = (HttpWebResponse)response.GetResponse();
StreamReader reader = webResponse.GetResponseStream();
objSR = new StreamReader(objStream, encode, true);
sResponse = objSR.ReadToEnd();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angularjs和ASP.NET Web Api在$ http.post上导致500 Internal Server Error - Angularjs and ASP.NET Web Api cause 500 Internal Server Error on $http.post Web Api和WCF中的HTTP发布内部服务器错误500 - Http Post Internal Server Error 500 in Web Api & WCF 如何解决HTTP Web request 500 Internal Server错误? - How to solve HTTP Web request 500 Internal Server error? 远程服务器返回错误:(500)内部服务器错误Web服务 - The remote server returned an error: (500) Internal Server Error Web service ASP .NET Web API:跟踪侦听器:HTTP / 1.1 500内部服务器错误:怎么了? - ASP .NET Web API: Trace Listeners: HTTP/1.1 500 Internal Server Error: What's wrong? 使用Web Api Http Put时内部服务器错误500 - Internal Server Error 500 when using Web Api Http Put 500(内部服务器错误),用于将发布请求发送到aspx页面 - 500 (Internal Server Error) for sending post request to aspx page MVC Web API POST实体(500内部服务器错误) - MVC Web API POST Entity (500 internal server error) Web服务响应返回500内部服务器错误 - Web Service Response returns a 500 Internal Server Error Web部署代理服务返回500内部服务器错误 - Web Deployment Agent Service returns a 500 Internal Server Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM