0

I have a program that utilize a web service. When this web service is tested on its own (run on ie AS .asmx) runs fine but when tried to be called from inside the web form it produces the following error:

Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'.

The web servce is:

    public Check1 () {

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

    [WebMethod]
    public void check2(string destination) 
    {
        Server.Transfer(destination);   
    }
}

And the web form from which is called is:

protected void Button1_Click1(object sender, EventArgs e)
{
    localhost.Check1 new2 = new localhost.Check1();
    new2.check2("Ipal_apoth_page.aspx");
}
user2466854
  • 17
  • 1
  • 6
  • Even though i am not sure why this should happen, it now says that "an object reference is required for the non-static field". – user2466854 Sep 06 '14 at 19:24
  • 1) I think you may have cut too much code out: `public Check1 () { }` is not valid. 2) What is the code meant to do? – Andrew Morton Sep 06 '14 at 19:53
  • Even if i comment the first part mothing changes. The code simply should redirect the user to another page, which works on its own (as mentioned) – user2466854 Sep 06 '14 at 20:06
  • You may want `Response.Redirect` instead: [Server.Transfer Vs. Response.Redirect](http://stackoverflow.com/questions/224569/server-transfer-vs-response-redirect). – Andrew Morton Sep 06 '14 at 20:32
  • I don't think you can use response.redirect inside a web service – user2466854 Sep 06 '14 at 20:50
  • Do you really need a web service to redirect to another page? Or is there something else going on which you haven't mentioned? – Andrew Morton Sep 06 '14 at 20:53
  • I am trying to practice on web services. So i stepped on this problem. Redirect though web service and i found it intriguing that even though i could use it on its own i could not consume it from inside a web form – user2466854 Sep 06 '14 at 21:14

1 Answers1

0

A web service is intended to be a data interface, not a web page server, which is why XML is expected. Web service protocols use XML format for their communications, e.g. WSDL or SOAP. The .aspx page you attempt to transfer the processing to will return HTML.

It may be that when you tried it in a browser, the browser was permissive enough to interpret the repsonse from the web service in the way you wanted even though that is not how it is meant to be used.

A better thing to practise with would be something simple like adding two numbers.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • I completely understand what you are saying and i have already tried two numbers and other things like that. I just don't understand their usage when concerning real life programs which don't require for example a simple mathematical exercise – user2466854 Sep 06 '14 at 22:09
  • @user2466854 If you want something more intricate to practise with, I suggest looking into AJAX to do partial updates of a web page. A very quick look at [Using jQuery to directly call ASP.NET AJAX page methods](http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/) suggests to me that it might interest you. – Andrew Morton Sep 06 '14 at 22:14