C# HttpWebRequest提交post请求

本文介绍了一个使用 C# 编写的简单 Web Service 的实现与调用过程。该 Web Service 提供了一个 doSearch 方法,接受三个字符串参数,并返回拼接后的结果。此外,还提供了一个 WinForm 客户端示例,演示了如何通过 HTTP 请求调用此 Web Service。
asmx代码
C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ WebService Language= "C#"  Class= "Service1"  %>
 
using  System;
using  System.Web;
using  System.Web.Services;
using  System.Web.Services.Protocols;
 
[WebService(Namespace =  "http://tempuri.org/" )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public  class  Service1  : System.Web.Services.WebService {
 
     [WebMethod]
   public  string  doSearch(String p1,String p2,String p3)
   {
         return  "Hello World"  + p1 + p2 + p3;
     }    
}


winform代码
C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
     private  void  button1_Click( object  sender, EventArgs e)
     {
       string  strURL =  "http://localhost:2852/WebSite1/Service1.asmx/doSearch" ;
       System.Net.HttpWebRequest request;
       request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strURL);
       //Post请求方式
       request.Method =  "POST" ;
       // 内容类型
       request.ContentType =  "application/x-www-form-urlencoded" ;
  
       //这是原始代码:
       string  paraUrlCoded =  "p1=x&p2=y&p3=测试的中文" ;
       byte [] payload;
       //将URL编码后的字符串转化为字节
       payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
       //设置请求的 ContentLength 
       request.ContentLength = payload.Length;
       //获得请 求流
       Stream writer = request.GetRequestStream();
       //将请求参数写入流
       writer.Write(payload, 0, payload.Length);
       // 关闭请求流
       writer.Close();
       System.Net.HttpWebResponse response;
       // 获得响应流
       response = (System.Net.HttpWebResponse)request.GetResponse();
       System.IO.Stream s;
       s = response.GetResponseStream();
       XmlDocument d =  new  XmlDocument();
       d.Load(s);
       MessageBox.Show(d.DocumentElement.InnerText);
 
     }
C#中,如果你使用HttpWebRequest发起HTTP请求并想要查看请求体(RequestBody),你可以按照以下步骤操作: 1. 首先,创建`HttpWebRequest`对象,并设置好你需要发送的URL和其他请求头信息。 ```csharp using System.Net; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/api/endpoint"); request.Method = "POST"; // 如果是POST请求 request.ContentType = "application/json"; // 根据实际需要设置内容类型 ``` 2. 对于POST、PUT等需要发送数据的请求,将请求体数据添加到`RequestStream`中。这通常通过读取文件流或者其他数据源来完成。 ```csharp byte[] requestBodyData = File.ReadAllBytes(@"path\to\your\file.txt"); // 读取本地文件作为例子 using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(requestBodyData, 0, requestBodyData.Length); } ``` 3. 确保连接完成后,你可以调用`GetResponse()`获取响应,然后从`WebResponse`对象的`ResponseStream`获取响应体。 ```csharp HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseBody = reader.ReadToEnd(); // 请求体的内容就在这里 Console.WriteLine(responseBody); // 打印出来或者处理它 } ``` 注意:这个过程通常适用于HTTP POST、PUT等请求,如果是GET请求,一般不需要手动设置请求体。另外,如果请求体不是字符串形式,记得关闭`StreamReader`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值