1

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,

Actually my issue is to call the webservice without using a webreference, and read the json file and parse it into a dictionary ,

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
            }


        }
    }
}
2
  • 1
    google has a lot of results using your exact title Commented Jul 16, 2013 at 15:52
  • no way? are you sure ? i had hours looking for answers, but actually i resolved by my self thank you Commented Jul 18, 2013 at 16:33

2 Answers 2

2

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
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

What is the purpose of your catch if you're just going to rethrow it? What about cleaning up resources like req and resp and sr? using() is your friend!
1

you can create a proxy class using wsdl utility or svcutil in visualstudiocommand prompt enter command wsdl.exe /out:[path and name of the file] /language:CS

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.