2013-11-13 4 views
2

Я попытался следующие, чтобы получить результат веб-службы:Вызов C# вебсервис от классического ASP

<% 
    Dim oRequest 
    Set oRequest = Server.CreateObject("MSXML2.ServerXMLHTTP") 

    oRequest.setOption(2) = SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS 
    oRequest.setTimeouts 10000, 10000, 10000, 10000 

    msURL = "http://[0.0.0.0]/contale/customer.asmx/GetInfoFromWhitesPages" 

    msSOAP = "<?xml version=""1.0"" encoding=""utf-8"" ?>" 
    msSOAP = msSOAP & "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">" 
    msSOAP = msSOAP & "<s:Body>" 
    msSOAP = msSOAP & "<GetInfoFromWhitesPagesResponse xmlns=""http://[0.0.0.0]/Contale/"">" 
    msSOAP = msSOAP & "<phone>4504656253</phone>" 
    msSOAP = msSOAP & "</GetInfoFromWhitesPagesResponse>" 
    msSOAP = msSOAP & "</s:Body>" 
    msSOAP = msSOAP & "</s:Envelope>" 

    oRequest.Open "POST", msURL, False 
    oRequest.setRequestHeader "Content-Type", "text/xml" 
    oRequest.setRequestHeader "SOAPAction", "[Some Url]" 
    oRequest.send msSOAP 
    Response.Write oRequest.ResponseBody 
%> 

Но показывает результаты есть: ?????????????? ??> ??? 4 ??????????????????????? † ??????????????????? ???????????????? †? 4 ??????????????????????????????? ????

Я также попытался следующие:

<html> 
<head><title></title></head> 
<body> 
<% 
    Dim objHTTP, strEnvelope 
    Set objHTTP = Server.CreateObject("Microsoft.XMLHTTP") 
    strEnvelope = "<?xml version='1.0' encoding='utf-8'?>" 
    strEnvelope = strEnvelope & "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" 
    strEnvelope = strEnvelope & "<soap:Body>" 
    strEnvelope = strEnvelope & "<GetInfoFromWhitesPagesResponse xmlns='http://[0.0.0.0]/Contale'>" 
    strEnvelope = strEnvelope & "<phone>4504656253</phone>" 
    strEnvelope = strEnvelope & "</GetInfoFromWhitesPagesResponse>" 
    strEnvelope = strEnvelope & "</soap:Body></soap:Envelope>" 

    Dim url 
    url = "http://[0.0.0.0]/contale/customer.asmx" 
    With objHTTP 
     .Open "post", url, False 
     .setRequestHeader "Content-Type", "text/xml; charset=utf-8" 
     .setRequestHeader "SOAPAction", "http://[0.0.0.0]/contale/GetInfoFromWhitesPagesResponse" 
     .send strEnvelope 
    End With 

    Dim strResponse 
    strResponse = objHTTP.responseXML.Text 
    If (strResponse = "") Then 
     Response.Write("Invalid user") 
    Else 
     Response.Write(strResponse) 
    End If 
%> 
</body> 
</html> 

Но, я получаю это:

soap:ClientSystem.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://[0.0.0.0]/contale/GetInfoFromWhitesPagesResponse. at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) 

Вот код моего WebService:

using System; 
using System.Net; 
using System.Text.RegularExpressions; 
using System.Web.Services; 

namespace CustomersInfo 
{ 
    /// <summary> 
    /// Summary description for WebService1 
    /// </summary> 
    [WebService(Namespace = "http://[0.0.0.0]/Contale/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class WebService1 : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public CustomerData GetInfoFromWhitesPages(string phone) 
     { 
      var customer = new CustomerData(); 

      var webClient = new WebClient(); 
      webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36 Other"); 

      var content =      webClient.DownloadString("http://www.whitepages.com/phone/" + phone); 

      const string infoStart = "<div class=\"address-card\">"; 

      if (content.Contains(infoStart)) 
      { 
       var startPos = content.IndexOf("<div class=\"address-card\">", StringComparison.Ordinal) + infoStart.Length; 
       var endPos = content.IndexOf("</div>", startPos, StringComparison.Ordinal); 
       var info = content.Substring(startPos, endPos - startPos).TrimEnd().TrimStart(); 

       info = info.Replace("\n", "").Replace("<p>", "").Replace("</p>", ""); 
       string[] tabInfo = Regex.Split(info, "<br />"); 
       string name = tabInfo[0]; 
       string address = tabInfo[2]; 
       string city = tabInfo[3].Split(' ')[0].Replace(",", ""); 
       string province = tabInfo[3].Split(' ')[1]; 
       string zip = tabInfo[3].Split(' ')[2] + " " + tabInfo[3].Split(' ')[3]; 

       customer.Name = name; 
       customer.Address = address; 
       customer.City = city; 
       customer.Province = province; 
       customer.Zip = zip; 
      } 
      return customer; 
     } 

     public struct CustomerData 
     { 
      public string Name; 
      public string Address; 
      public string City; 
      public string Province; 
      public string Zip; 
     } 
    } 
} 

Любая помощь будет быть оцененным.

Спасибо

+0

Несколько мыслей: попробуйте Server.CreateObject («MSXML2.ServerXMLHTTP.6.0»), а также проверьте кодировку вашего классического ASP страница результатов. Откройте «Блокнот» (или только любой текстовый редактор, отличный от VS), выберите «Сохранить как» в меню «Файл», и если в поле «Кодировка» выбран ASCI, измените его на UTF-8 – John

+0

Кроме того, маловероятно, что запрос требует параметр 'GetInfoFromWhitesPagesResponse'. Скорее я ожидаю, что это произойдет внутри ответа, и запрос должен просто использовать 'GetInfoFromWhitesPages'. Что касается недопустимого SOAPAction - прочитайте правильное значение из WSDL службы. –

+0

Благодаря вам, я сделал небольшую модификацию для своего второго скрипта и смог добиться того, что искал. – Laskar78

ответ

0

Сначала добавьте кодовая = 65001 Второй сказать браузеру, что вы отправляете в кодировке UTF-8

? Отображаются символы, потому что браузер, а также сервер не знают, как обрабатывать ответ

Смежные вопросы