jxlaoxu

C# URL访问和获取返回值

最近在做C#的URL访问和获取返回值,期间遇到各种问题和坑,把自己的问题贴出和解决方法给出,给各位看客借鉴。

我的项目中要连续两个URL,第一个先登录并获取相应的Cookie认证,然后再利用一个URL获取相关的信息。我的做法如下:

/// <summary>
/// 获取strBuff 的返回值
/// </summary>
/// <param name="loginurl" name="ipurl">

private CookieContainer container = new CookieContainer();
CookieContainer containertemp = null;


public string GetLoginURLInfo(string loginurl,string ipurl)//loginurl这个是登录的URL 例如:http://192.168.7.106:89/jf/platform/login/vali?                                                                                       //username=XXXXXX&password=XXXXXXX,ipurl这个是获取登录的IP和端口 例                                                                                         //如:http://192.168.7.106:89
{
string strBuff = "";
try
{
Uri httpURL = new Uri(loginurl);
HttpWebRequest httpReq = WebRequest.Create(httpURL) as HttpWebRequest;//创建URL访问还有一种是

//(HttpWebRequest)WebRequest.Create(httpURL) 这种方式,但是用了之后总是报错,我这边就改了

httpReq.Method = "POST";
httpReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
httpReq.CookieContainer = container;//保存Cookie认证
httpReq.Accept = "*/*";
httpReq.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
//container.Add(httpResp.Cookies);
containertemp = httpReq.CookieContainer;
Stream respStream = httpResp.GetResponseStream();
StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
strBuff = respStreamReader.ReadToEnd();//获取到登录后的返回值

//System.Diagnostics.Trace.WriteLine("GetLoginURLInfo strBuff=:" + strBuff);

if (strBuff.Contains("success"))
{
DateTime dtnow = DateTime.Now;//获取当前时间
string strNowDate = dtnow.ToString("yyyy-MM-dd HH:mm:ss");
string streambuff = "pageno=0&endTime=" + strNowDate+ "&proStatus=1";//URL里面嵌入参数,这个我也是想了好久,看了好多别人的介绍才知道的

httpReq = WebRequest.Create(ipurl+"jf/api/alarm/queryAlarmList") as HttpWebRequest;//http://192.168.7.106:89/jf/api/alarm/queryAlarmList

//httpReq.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//httpReq.KeepAlive = true;
httpReq.CookieContainer = containertemp;
httpReq.Method = "POST";
httpReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
httpReq.ContentType = "application/x-www-form-urlencoded";

byte[] bytefuff = Encoding.UTF8.GetBytes(streambuff);
httpReq.ContentLength = bytefuff.Length;
Stream stream = httpReq.GetRequestStream();
stream.Write(bytefuff, 0, bytefuff.Length);//URL参数转化格式
//httpReq.GetRequestStream().Write(bytefuff, 0, bytefuff.Length);

httpResp = (HttpWebResponse)httpReq.GetResponse();

respStream = httpResp.GetResponseStream();
respStreamReader = new StreamReader(respStream, Encoding.UTF8);
strBuff = respStreamReader.ReadToEnd();//获取到最终的结果,哈哈

//System.Diagnostics.Trace.WriteLine("GetAlarmURLInfo strBuff=:" + strBuff);
}
}
catch (System.Exception ex)
{
System.Diagnostics.Trace.WriteLine("出错,详细信息:" + ex.Message);
}
return strBuff;//返回最终的结果
}

posted on 2016-11-28 16:49  jxlaoxu  阅读(918)  评论(0)    收藏  举报

导航