2013-07-16 88 views
1

我想知道是否有人使用一個類來調用WebService,這個WS在組織引用後收到一個整數並響應成一個json文件,如何在不使用WebReference的情況下調用WebService?

其實我的問題是調用webservice而不使用一個Web引用,並讀取JSON文件,並將其解析成一個字典,

我感謝你的幫助

最好的問候,我讓你我的代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Net; 
using Dimex.ChangeSAP.Core.Utilities; 

namespace Dimex.ChangeSAP.Core.Utilities 
{ 
    class ConsumirWebService 
    { 
     public void ConsumirWS() 
     { 

      Dimex.ChangeSAP.Core.Entities.Seguridad.Usuario users = new Dimex.ChangeSAP.Core.Entities.Seguridad.Usuario(); 
      int idUsuaro = users.IdUsuario; 

      try 
      { 

       System.Net.WebRequest req = System.Net.WebRequest.Create("http://192.168.8.97/PassportPruebas/api/partners?enterprise_system_id=1&organizational_reference=" + idUsuaro); 
       //req.Proxy = new System.Net.WebProxy(ProxyString, true); 
       //Add these, as we're doing a POST 
       req.ContentType = "application/x-www-form-urlencoded"; 
       req.Method = "POST"; 
       //We need to count how many bytes we're sending. 
       //Post'ed Faked Forms should be name=value& 
       string postData = "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=90BA&INPUT_DATA=" + sendXML; 
       byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData); 
       req.ContentLength = bytes.Length; 
       System.IO.Stream os = req.GetRequestStream(); 
       os.Write(bytes, 0, bytes.Length); //Push it out there 
       os.Close(); 
       System.Net.WebResponse resp = req.GetResponse(); 
       if (resp == null) 
       { 
        return null; 
       } 
       System.IO.StreamReader sr = 
         new System.IO.StreamReader(resp.GetResponseStream()); 

       string respuesta = sr.ReadToEnd().Trim(); 
       return respuesta; 

      } 
      catch (Exception ex) 
      { 
       return ""; 
       //throw or return an appropriate response/exception 
      } 


     } 
    } 
} 
+1

谷歌有很多結果使用您的確切名稱 – Jonesopolis

+0

沒辦法?你確定 ?我有幾個小時尋找答案,但實際上我自己解決了我的謝謝 – daevst

回答

1

您可以創建一個代理類使用wsdl實用程序或svcutil在visualstudiocommand中提示輸入命令wsdl.exe/out:[文件的路徑和名稱] /語言:CS

2

呃其實這裏是我的代碼,對於這類問題的人也是,

 public static string LlamarWebService(string url) 
     { 

      try 
      { 
       System.Net.WebRequest req = System.Net.WebRequest.Create(url); 

       req.ContentType = "application/json"; 
       req.Method = "GET"; 

       System.Net.WebResponse resp = req.GetResponse(); 
       if (resp == null) return null; 
       System.IO.StreamReader sr = 
         new System.IO.StreamReader(resp.GetResponseStream()); 

       string respuesta = sr.ReadToEnd().Trim(); 
       return respuesta; 

      } 
      catch (Exception ex) 
      { 
       throw ex; 
       // return ""; 
       //throw or return an appropriate response/exception 
      } 
     } 
+0

如果你只是要重新拋出它,你的捕獲的目的是什麼?如何清理req和resp和sr等資源?使用()是你的朋友! – n8wrl

相關問題