好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#使用HttpPost请求调用WebService的方法

之前调用 webservice 都是直接添加服务引用,然后调用 webservice 方法的,最近发现还可以使用 http 请求调用 webservice。这里还想说一句,还是 web api 的调用简单。

webservice 服务端代码:

?

public class webservicedemo : system.web.services.webservice

  {

 

   [webmethod]

   public string helloworld()

   {

    return "hello world" ;

   }

 

   [webmethod]

   public string sum( string param1, string param2)

   {

    int num1 = convert.toint32(param1);

    int num2 = convert.toint32(param2);

 

    int sum = num1 + num2;

 

    return sum.tostring();

   }

  }

很简单的代码,只是用于演示。 

客户端调用代码:

?

class program

  {

   static void main( string [] args)

   {

    program program = new program();

    string url = "http://localhost:12544/webservicedemo.asmx" ;

    string method = "sum" ;

    string num1 = "1" ;

    string num2 = "2" ;

 

    string result = program.httppostwebservice(url, method, num1, num2);

 

    console.writeline(result);

    console.readkey();

   }

 

   public string httppostwebservice( string url, string method, string num1, string num2)

   {

    string result = string .empty;

    string param = string .empty;

    byte [] bytes = null ;

 

    stream writer = null ;

    httpwebrequest request = null ;

    httpwebresponse response = null ;

 

    param = httputility.urlencode( "param1" ) + "=" + httputility.urlencode(num1) + "&" + httputility.urlencode( "param2" ) + "=" + httputility.urlencode(num2);

    bytes = encoding.utf8.getbytes(param);

 

    request = (httpwebrequest)webrequest.create(url + "/" + method);

    request.method = "post" ;

    request.contenttype = "application/x-www-form-urlencoded" ;

    request.contentlength = bytes.length;

 

    try

    {

     writer = request.getrequeststream();  //获取用于写入请求数据的stream对象

    }

    catch (exception ex)

    {

     return "" ;

    }

 

    writer.write(bytes, 0, bytes.length);  //把参数数据写入请求数据流

    writer.close();

 

    try

    {

     response = (httpwebresponse)request.getresponse();  //获得响应

    }

    catch (webexception ex)

    {

     return "" ;

    }

 

    #region 这种方式读取到的是一个返回的结果字符串

    stream stream = response.getresponsestream();  //获取响应流

    xmltextreader reader = new xmltextreader(stream);

    reader.movetocontent();

    result = reader.readinnerxml();

    #endregion

 

    #region 这种方式读取到的是一个xml格式的字符串

    //streamreader reader = new streamreader(response.getresponsestream(), encoding.utf8);

    //result = reader.readtoend();

    #endregion

 

    response.dispose();

    response.close();

 

    //reader.close();

    //reader.dispose();

 

    reader.dispose();

    reader.close();

 

    stream.dispose();

    stream.close();

 

    return result;

   }

  }

第一种读取方式的返回结果:

第二种读取方式的返回结果:

ps:如果遇到调用时报错,可以尝试在服务端(即webservice)的 web.config 配置中添加如下配置节点。

?

< system.web >

  < webservices >

   < protocols >

   < add name = "httppost" />

   </ protocols >

  </ webservices >

  </ system.web >

参考: C#使用Http Post方式传递Json数据字符串调用Web Service

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

dy("nrwz");

查看更多关于C#使用HttpPost请求调用WebService的方法的详细内容...

  阅读:60次