2015-12-02 104 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都可以工作。