HttpWebRequest
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpWebRequest相关的知识,希望对你有一定的参考价值。
同步请求=====================================================================================
byte[] data = System.Text.Encoding.UTF8.GetBytes("data=Hello,World~");//组建要提交的数据
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8077/Index.aspx");
//请求报文的参数设置
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";//post请求的时候,必须加上这行代码,不然数据无法提交到目标url
using (Stream requestStream = request.GetRequestStream())//请求流;有数据需要传递给目标url时候,需要把数据放在此流中
{
requestStream.Write(data, 0, data.Length);
}//get类型的请求,不能有【请求流】get请求如果有参数,把参数 拼接到 url中 .create()
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();//返回流
StreamReader sr = new StreamReader(responseStream,Encoding.UTF8);
string result = sr.ReadToEnd();//读取流,得到String
【从服务器获取cookie】
byte[] data = System.Text.Encoding.UTF8.GetBytes("data=Hello,World~");//组建要提交的数据
【从服务器获取cookie】
byte[] data = System.Text.Encoding.UTF8.GetBytes("data=Hello,World~");//组建要提交的数据
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8077/Index.aspx");
//请求报文的参数设置
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";//post请求的时候,必须加上这行代码,不然数据无法提交到目标url
request.CookieContainer = new CookieContainer();//请求地址,获取cookie 如 登录时的cookie
using (Stream requestStream = request.GetRequestStream())//请求流;有数据需要传递给目标url时候,需要把数据放在此流中
{
requestStream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();//返回流
StreamReader sr = new StreamReader(responseStream,Encoding.UTF8);
string result = sr.ReadToEnd();//读取流,得到String
Cookie ck = response.Cookies[0];
if (ck!=null)
{
Response.Write(ck.Value);
}
【组建cookie提交个服务器】
byte[] data = System.Text.Encoding.UTF8.GetBytes("data=Hello,World~");//组建要提交的数据
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8077/Index.aspx");
//请求报文的参数设置
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";//post请求的时候,必须加上这行代码,不然数据无法提交到目标url
request.CookieContainer = new CookieContainer();//使用cookie就需要加上此行代码
Cookie c = new Cookie("name","zs");
c.Domain = "localhost";//不能就加http 和端口号 只能放域名
request.CookieContainer.Add(c);
using (Stream requestStream = request.GetRequestStream())//请求流;有数据需要传递给目标url时候,需要把数据放在此流中
{
requestStream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();//返回流
StreamReader sr = new StreamReader(responseStream,Encoding.UTF8);
string result = sr.ReadToEnd();//读取流,得到String
异步请求======================================================================================
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AsynHttpWebRequest
{
public partial class Index : System.Web.UI.Page
{
public string TestStr;
protected void Page_Load(object sender, EventArgs e)
{
DoAsynVisist();
TestStr = "外";
}
private void DoAsynVisist()
{
string data = "data=Hello World~";//模拟要传递的数据
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8044/Index.aspx");
request.Method = "post";
request.ContentType = "application/x-www-form-urlencoded";//post请求的时候,必须加上这行代码,不然数据无法提交到目标url
List<object> saveData = new List<object>() { data, request };
request.BeginGetRequestStream(GetRequestStreamCallBack, saveData);//开始异步
}
catch
{//异常处理
}
}
private void GetRequestStreamCallBack(IAsyncResult ar)
{
List<object> saveData = ar.AsyncState as List<object>;//转换要传递的data,和Request对象
string data = saveData[0] as string;//要传递的数据
byte[] byteData = System.Text.Encoding.UTF8.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)saveData[1];
Stream requestStream = request.EndGetRequestStream(ar);//请求流
requestStream.Write(byteData, 0, byteData.Length);
requestStream.Close();
request.BeginGetResponse(GetResponseCallBack,request);//一旦使用异步处理,所有的操作方法都要配套,像这里使用BeginGetResponse而不是直接GetResponse
}
private void GetResponseCallBack(IAsyncResult ar)
{
HttpWebRequest request = ar.AsyncState as HttpWebRequest;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
Stream streamResponse = response.GetResponseStream();//返回流
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();//得到结果
//模拟处理
//模拟处理
string filePath = AppDomain.CurrentDomain.BaseDirectory + "/Result";//System.Web.HttpContext.Current.Server.MapPath("/Result"); //HttpContext.Current.Request.MapPath("/Result");//异步这里为null
TestStr = "内";
File.WriteAllText(filePath + "\\" + "1.txt", responseString + TestStr);
streamResponse.Close();
streamRead.Close();
response.Close();
}
}
}
以上是关于HttpWebRequest的主要内容,如果未能解决你的问题,请参考以下文章