ASP.NET-C# Post 一般处理程序(ashx)并得到返回值

var postUrl = "http://xxx.com/xxp/LoginInfo.ashx";
			var postString = "method=CheckPW&id=4454556289&pwd=&checkword=8888&sign=";
			HttpWebRequest httpRequset = null;
			HttpWebResponse httpResponse = null;
			try
			{
				//POST登录信息获取返回结果,结果为0是为登录成功
				httpRequset = (HttpWebRequest)HttpWebRequest.Create(postUrl);//创建http 请求
				httpRequset.Method = "POST";//POST 提交
				httpRequset.ContentType = "application/x-www-form-urlencoded";//以上信息在监听请求的时候都有的直接复制过来
				httpRequset.Headers.Add("X-Requested-With", "XMLHttpRequest");
				httpRequset.Accept = "*/*";
				httpRequset.Referer = "http://xxx.com/slip/Left.aspx";
				httpRequset.Headers.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.7,ja;q=0.3");
				httpRequset.Headers.Add("Accept-Encoding", "gzip, deflate");
				httpRequset.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
				httpRequset.KeepAlive = true;
				httpRequset.Host = "xxx.com";
				//httpRequset.Headers.Add("Access-Control-Allow-Origin", "*");//可跨域
				httpRequset.Headers.Add("Pragma", "no-cache");
				httpRequset.Headers.Add("DNT", "1");
				httpRequset.ServicePoint.Expect100Continue = false;
				byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postString);
				httpRequset.ContentLength = bytes.Length;
				Stream stream = httpRequset.GetRequestStream();
				stream.Write(bytes, 0, bytes.Length);
				stream.Close();//以上是POST数据的写入

				httpResponse = (HttpWebResponse)httpRequset.GetResponse();//获得 服务端响应
				var s = string.Empty;
 				using (Stream responsestream = httpResponse.GetResponseStream())
				{
					using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.UTF8))
					{
						content = sr.ReadToEnd();   //返回信息
					 
					}

WebClient 写法

WebClient webClient = new WebClient();
 
         NameValueCollection formData = new NameValueCollection();
         formData["username"] = "testuser";
         formData["password"] = "mypassword";
 
         byte[] responseBytes = webClient.UploadValues(postUrl , "POST", formData);
         string responsefromserver = Encoding.UTF8.GetString(responseBytes);
         Console.WriteLine(responsefromserver);
         webClient.Dispose();


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值