C#中使用HttpPost调用WebService的方法_2

转载地址https://www.jb51.net/article/242334.htm

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C# 使用 HttpPost 请求调用 WebService,可以按照以下步骤进行操作: 1. 首先,确保你的 C# 项目引用了 `System.Net.Http` 命名空间。 2. 创建一个 `HttpClient` 对象,用于发送 Http 请求。可以使用以下代码创建一个 `HttpClient` 对象: ```csharp HttpClient client = new HttpClient(); ``` 3. 设置要调用WebService 的 URL。假设 WebService 的 URL 是 `http://example.com/your_webservice`,可以使用以下代码设置 URL: ```csharp string url = "http://example.com/your_webservice"; ``` 4. 构建请求参数。根据你要调用的具体 WebService 方法的要求,构建相应的请求参数。可以使用 `FormUrlEncodedContent` 类来构建 URL 编码的请求参数。例如: ```csharp var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" } }; var content = new FormUrlEncodedContent(parameters); ``` 5. 发送 HttpPost 请求并获取响应。使用 `client.PostAsync` 方法发送 HttpPost 请求,并使用 `await` 关键字等待响应。例如: ```csharp HttpResponseMessage response = await client.PostAsync(url, content); ``` 6. 处理响应。根据你的需求,可以从响应获取内容、状态码等信息。例如,可以使用以下代码获取响应内容: ```csharp string responseContent = await response.Content.ReadAsStringAsync(); ``` 这样,你就可以在 C# 使用 HttpPost 请求调用 WebService。根据实际情况,可能还需要处理异常、设置请求头等操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值