简体   繁体   English

如何在不使用WebReference的情况下调用WebService?

[英]How to Call a WebService without using WebReference?

I want to know if there is someone who had use a Class to Call a WebService, this WS receives an integer after the organizational references and responds into a json file, 我想知道是否有人使用类来调用WebService,此WS在组织引用后收到一个整数并响应json文件,

Actually my issue is to call the webservice without using a webreference, and read the json file and parse it into a dictionary , 实际上我的问题是在不使用webreference的情况下调用webservice,并读取json文件并将其解析为字典,

I appreciate your help 我感谢您的帮助

Best Regards, i let you my code 最诚挚的问候,我告诉你我的代码

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
            }


        }
    }
}

Well Actually here is my code for anyone with this kind of issue too, 那么实际上这里是我的代码也适用于任何有此类问题的人,

        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
            }
        }

您可以在visualstudiocommand提示符下使用wsdl实用程序或svcutil创建代理类,输入命令wsdl.exe / out:[文件的路径和名称] /语言:CS

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM