2015-12-02 60 views
0

我正在拉我的头发。我无法使用POST和JSON成功调用.NET Web服务(“远程服务器返回错误:(500)内部服务器错误”)。如果我不坚持使用JSON或使用GET,我可以使其工作。我在Xamarin Studio中完成了这一切,因此Web服务器是XSP而不是IIS,如果这有所帮助的话。使用POST和JSON调用ASP.NET Web服务时出错

任何帮助是很多赞赏。

这里是我的web服务代码:

using System; 
using System.Web; 
using System.Web.Services; 
using System.Web.Script.Services; 
using System.Web.Script.Serialization; 

namespace WebServiceTest 
{ 
    [WebService (Namespace = "http://tempuri.org/WebServiceTest")] 
    [ScriptService] 
    public class API : System.Web.Services.WebService 
    { 

     [WebMethod] 
     [ScriptMethod (UseHttpGet=true, ResponseFormat=ResponseFormat.Json)] 
     public string About() { 
      return "About WebServiceTest"; 
     } 

    } 
} 

...这里是我的web.config ...

<?xml version="1.0"?> 
<!-- 
Web.config file for WebServiceTest. 

The settings that can be used in this file are documented at 
http://www.mono-project.com/Config_system.web and 
http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx 
--> 
<configuration> 
    <system.web> 
    <webServices> 
     <protocols> 
     <add name="HttpGet" /> 
     <add name="HttpPost" /> 
     </protocols> 
    </webServices> 
    <compilation defaultLanguage="C#" debug="true"> 
     <assemblies> 
     </assemblies> 
    </compilation> 
    <customErrors mode="RemoteOnly"> 
    </customErrors> 
    <authentication mode="None"> 
    </authentication> 
    <authorization> 
     <allow users="*" /> 
    </authorization> 
    <httpHandlers> 
    </httpHandlers> 
    <trace enabled="false" localOnly="true" pageOutput="false" requestLimit="10" traceMode="SortByTime" /> 
    <sessionState mode="InProc" cookieless="false" timeout="20" /> 
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" /> 
    <pages> 
    </pages> 
    </system.web> 
</configuration> 

...这里是我的应用程序测试代码。 ..

using System; 
using System.IO; 
using System.Net; 
using System.Text; 

namespace WebServiceTestApp 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      Console.WriteLine ("Starting..."); 

      Console.WriteLine ("Making API call..."); 
      string url = "http://127.0.0.1:8080/API.asmx/About"; 
      HttpWebRequest request; 

      // Test 1: Use GET, don't set content type or content 
      // Works, returns XML 
      Console.WriteLine ("Test 1"); 
      request = (HttpWebRequest)WebRequest.Create (url); 
      GetResponse (request); 

      // Test 2: Use GET, set content type but no content 
      // Works, returns JSON 
      Console.WriteLine ("Test 2"); 
      request = (HttpWebRequest)WebRequest.Create (url); 
      request.ContentType = "application/json; charset=utf-8"; 
      GetResponse (request); 

      // Test 3: Use POST, don't set content type or content 
      // Works, returns XML 
      Console.WriteLine ("Test 3"); 
      request = (HttpWebRequest)WebRequest.Create (url); 
      request.Method = "POST"; 
      GetResponse (request); 

      // Test 4: Use POST, set content type but no content 
      // *** Fails: 500 Internal Server Error 
      Console.WriteLine ("Test 4"); 
      request = (HttpWebRequest)WebRequest.Create (url); 
      request.ContentType = "application/json; charset=utf-8"; 
      request.Method = "POST"; 
      GetResponse (request); 

      // Done. 
      Console.WriteLine ("Done!"); 

     } 

     public static void GetResponse(HttpWebRequest request) 
     { 
      try { 
       using (var response = (HttpWebResponse)request.GetResponse()) { 
        var stream = response.GetResponseStream(); 
        var reader = new StreamReader (stream); 
        var result = reader.ReadToEnd(); 
        Console.WriteLine (result); 
        reader.Close(); 
        reader.Dispose(); 
        response.Close(); 
       } 
      } catch (Exception e) { 
       Console.WriteLine ("*** Failed: Error '" + e.Message + "'."); 
      } 
     } 

    } 
} 

,如果我尝试添加任何内容到POST,请求,即与替换测试4的测试代码也会失败...

// Test 4: Use POST, set content type and content 
    // *** Fails: 500 Internal Server Error 
    Console.WriteLine ("Test 4"); 
    request = (HttpWebRequest)WebRequest.Create (url); 
    request.Method = "POST"; 
    request.ContentType = "application/json; charset=utf-8"; 
    var paramData = ""; // also fails with "{}" 
    request.ContentLength = paramData.Length; 
    using (var writer = new StreamWriter (request.GetRequestStream())) { 
     writer.Write (paramData); 
    } 
    GetResponse (request); 

如果我在ScriptMethod中更改“UseHttpGet = false”,它也会失败。

+2

为什么你使用过时的经典ASMX Web服务?为什么不使用WCF或WebAPI?在进行POST时,您还会发布什么内容?您是否检查过错误日志对内部服务器错误的说明?最有可能您要发送的数据无效或无效方法 –

+0

https://bugzilla.xamarin。 com/show_bug.cgi?id = 34011该源代码可用,最近有很多MS参考源被引入,分叉,调试并发出拉请求......在个人记录中,我同意@Ahmedilyas,去WCF/WebApi ... – SushiHangover

+1

500的意思是“检查日志找到真正的错误“ – Jason

回答

0

我解决了我自己的问题。对于XSP和IISExpress(我还没有尝试过任何其他Web服务器),您无法对GET和POST同时使用contentType =“application/json”的WebMethod。 (是的,我知道,我甚至不应该试图...)也就是说,如果你包含“(UseHttpGet = true”,你可以获取方法但不是POST,如果你不包含参数,你可以POST但不GET。有趣的是,如果你没有设置内容类型(即你愿意使用XML),GET和POST都可以工作。