webservice client to save resulting xml

webservice client to save resulting xml

Page 1 of 1

12 Replies - 8374 Views - Last Post: 17 August 2010 - 10:19 AM Rate Topic: -----

#1 Best123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 05-August 10

webservice client to save resulting xml

Posted 13 August 2010 - 09:56 AM

I created a .net C# web service that takes a casenum as input and gives out results in a dataset as xml. I want to create a webservice client that takes the casenum input, calls the webservice with that casenum and saves the resulting xml locally for further processing.

I know how to create a web client that shows the resulting data set in a grid view but cant figure how to create a console client that will save the resulting xml.

copied below is the code for my web client.

Please guide to modify this for the console client that saves the xml.

webclient code:
namespace ResultsWebServicesClient
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        private void BindDataToGrid(string CaseNum)
        {
            FG_Results results = new FG_Results();
            DataSet ds1 = new DataSet();
            ds1 = results.Get_FG_Results(CaseNum);
            GridView1.AutoGenerateColumns = true;
            GridView1.DataSource = ds1.Tables[0];
            GridView1.DataBind();
        }

        protected void GetResults(object sender, EventArgs e)
        {
            BindDataToGrid(CaseNo.Text);
        }

    }
}



web service code:
[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 FG_Results : System.Web.Services.WebService
{

    public FG_Results()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod(Description = "Returns FG Results by CaseNum")]
    public DataSet Get_FG_Results(string CaseNum)
    {
        String myConn = ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString();
        SqlConnection conn = new SqlConnection(myConn);
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet dsInfo = new DataSet();
        string query = "SSRS.FlowComponentGroupDisplay"; 
        try
        {
            cmd.CommandText = query;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@CaseNo", CaseNum));
            cmd.Connection = conn;          
            adapter.SelectCommand = cmd;        
            adapter.Fill(dsInfo, "FG_Results");          
            return dsInfo;
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write(ex.Message);
            return null;
        }
        finally
        {
            conn.Close();
        }
    }

}




Is This A Good Question/Topic? 0
  • +

Replies To: webservice client to save resulting xml

#2 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: webservice client to save resulting xml

Posted 13 August 2010 - 10:20 AM

.NET is actually obfuscating the XML from you. It does all the serialization and deserialization for you. So to you, it's as if there never was any XML. You start with a DataSet on one side, then end with one on the other.

However, DataSet has a method called WriteXML that will let you save the dataset as XML. So you can use that on the result of your WebService call.
Was This Post Helpful? 0
  • +
  • -

#3 Best123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 05-August 10

Re: webservice client to save resulting xml

Posted 13 August 2010 - 10:51 AM

Oh...I See. In that case Do you have suggestions to modify my web method (webservice) so that I get the raw results in the xml instead of the dataset in the xml?

Thank you very much for your help.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: webservice client to save resulting xml

Posted 13 August 2010 - 11:06 AM

Not that I know of. The whole point of the .NET implementation of web services is so that you don't have to deal with the raw XML.

You could skip the whole Web Service Reference part and use a WebClient or an HttpWebRequest to treat it like it's just any other web call.

But I'd also suggest trying the WriteXML method I linked for you. That might solve your problem in a simple manner.
Was This Post Helpful? 0
  • +
  • -

#5 Best123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 05-August 10

Re: webservice client to save resulting xml

Posted 16 August 2010 - 05:20 AM

View PostinsertAlias, on 13 August 2010 - 09:20 AM, said:

.NET is actually obfuscating the XML from you. It does all the serialization and deserialization for you. So to you, it's as if there never was any XML. You start with a DataSet on one side, then end with one on the other.

However, DataSet has a method called WriteXML that will let you save the dataset as XML. So you can use that on the result of your WebService call.


So, Ho do I code my webservice (web method) so that it does not obfuscate data in a data set? For some reasson I do not want to use the writeXML method of the DataSet.

Thank You.
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: webservice client to save resulting xml

Posted 16 August 2010 - 07:09 AM

Consume the web service using the HttpWebRequest and HttpWebResponse classes. This will return the raw XML.

Calling Web Service using HttpWebRequest
Was This Post Helpful? 0
  • +
  • -

#7 Best123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 05-August 10

Re: webservice client to save resulting xml

Posted 16 August 2010 - 07:37 AM

View PostJayman, on 16 August 2010 - 06:09 AM, said:

Consume the web service using the HttpWebRequest and HttpWebResponse classes. This will return the raw XML.

Calling Web Service using HttpWebRequest


Thank You. Can you give an example?
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: webservice client to save resulting xml

Posted 16 August 2010 - 07:42 AM

He already linked you one.
Was This Post Helpful? 0
  • +
  • -

#9 Best123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 05-August 10

Re: webservice client to save resulting xml

Posted 16 August 2010 - 08:26 AM

View PostJayman, on 16 August 2010 - 06:09 AM, said:

Consume the web service using the HttpWebRequest and HttpWebResponse classes. This will return the raw XML.

Calling Web Service using HttpWebRequest


Ok I tried calling my WebService using HttpWebRequest, but I get Error:The remote server returned an error: (500) Internal Server Error.

I can access my webservice using the uri:http://localhost/ResultsWebServices/FG_Results.asmx?op=Get_FG_Results
I enter a caseNum, Click Invoke and get results successfully. But when I call it using a console client (code copied below), It gives this error. I have added a Services Refrence to Get_FG_Results for the console client.
Console client code:
namespace FG_Results_Client
{
    class FG_Results
    {
        static void Main(string[] args)
        {
            ProcessGetHttpRequest();
        }

        private static void ProcessGetHttpRequest()
        {

            Uri myUri = new Uri("http://localhost/ResultsWebServices/FG_Results.asmx/SendRequest?CaseNum=FG10-006294");

            HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(myUri);
            HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();//Error on this line "The remote server returned an error: (500) Internal Server Error"
            Console.WriteLine();
            if (HttpWResp.StatusCode == HttpStatusCode.OK)
                Console.WriteLine("\nRequest succeeded and the requested information is in the response  ,Description : {0}", HttpWResp.StatusDescription);
            if (myUri.Equals(HttpWResp.ResponseUri))
                Console.WriteLine("\nThe Request Uri was not redirected by the server");
            else
                Console.WriteLine("\nThe Request Uri was redirected to :{0}", HttpWResp.ResponseUri);
            Console.WriteLine("Content length is {0}", HttpWResp.ContentLength);
            Console.WriteLine("Content type is {0}", HttpWResp.ContentType);
            Console.WriteLine("Protocol Version is {0}", HttpWResp.ProtocolVersion.ToString());

            // Get the stream associated with the response.

            Stream receiveStream = HttpWResp.GetResponseStream();
            // Pipes the stream to a higher level stream reader with the required encoding format.

            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            Console.WriteLine();
            Console.WriteLine(readStream.ReadToEnd());
            //Insert your code there
            HttpWResp.Close();
            readStream.Close();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: webservice client to save resulting xml

Posted 16 August 2010 - 01:02 PM

The URI is incorrect.

SendRequest should be the name of your web method in the web service. In this case, it should be Get_FG_Results.
Should be:
Uri myUri = new Uri("http://localhost/ResultsWebServices/FG_Results.asmx/Get_FG_Results?CaseNum=FG10-006294");


Make that change and give it a try.
Was This Post Helpful? 0
  • +
  • -

#11 Best123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 05-August 10

Re: webservice client to save resulting xml

Posted 16 August 2010 - 01:39 PM

View PostJayman, on 16 August 2010 - 12:02 PM, said:

The URI is incorrect.

SendRequest should be the name of your web method in the web service. In this case, it should be Get_FG_Results.
Should be:
Uri myUri = new Uri("http://localhost/ResultsWebServices/FG_Results.asmx/Get_FG_Results?CaseNum=FG10-006294");



Make that change and give it a try.


I did make that change and tested it, but I get the error again.
Uri myUri = new Uri("http://localhost/ResultsWebServices/FG_Results.asmx/Get_FG_Results?CaseNum=FG10-006294");

Error: The remote server returned an error: (500) Internal Server Error.
Line 26: HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();

Stack Trace:


[WebException: The remote server returned an error: (500) Internal Server Error.]
System.Net.HttpWebRequest.GetResponse() +5375213
ResultsWebServicesWebClient._Default.ProcessGetHttpRequest() in C:\Inetpub\wwwroot\ResultsWebServicesWebClient\Default.aspx.cs:26
ResultsWebServicesWebClient._Default.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\ResultsWebServicesWebClient\Default.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.onload(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: webservice client to save resulting xml

Posted 16 August 2010 - 05:20 PM

What happens when you paste the URL directly into the address bar of your browser?

http://localhost/ResultsWebServices/FG_Results.asmx/Get_FG_Results?CaseNum=FG10-006294

Was This Post Helpful? 0
  • +
  • -

#13 Best123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 05-August 10

Re: webservice client to save resulting xml

Posted 17 August 2010 - 10:19 AM

View PostJayman, on 16 August 2010 - 04:20 PM, said:

What happens when you paste the URL directly into the address bar of your browser?

http://localhost/ResultsWebServices/FG_Results.asmx/Get_FG_Results?CaseNum=FG10-006294


Internet Explorer cannot display the webpage

What you can try:
It appears you are connected to the Internet, but you might want to try to reconnect to the Internet.

Retype the address.

Go back to the previous page.

Most likely causes:
•You are not connected to the Internet.
•The website is encountering problems.
•There might be a typing error in the address.

This address shows me Get_FG_Results web service:
http://localhost/Res...=Get_FG_Results
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1