如果是在后台通讯,就利用HttpWebResponse.Cookies这个属性,获得返回的Cookie类(Cookies本身就是Cookie的一个集)。然后将这个Cookie写到给用户的Cookie内。大致流程如下:Web Browser ------ Web Server ---------- Backend Server 请求-------------> 从请求中读取Cookie--> 利用HttpWebRequest.CookieContainer将Cookie加入 回复<------------- 设置回复的Cookie <--- 利用HttpWebResponse.Cookies获得后台回复的Cookie集合
URL我写成http://www.xxxxx/servlet?参数名=参数
那些参数名是从网页原码里的form里找出来的
{
Encoding encoding = Encoding.GetEncoding("GB2312");
StringBuilder sb = new StringBuilder();
foreach (string s in content.Keys)
{
sb.Append(s+"="+content[s]+"&");
}
if (sb.Length>0)
{
sb.Remove(sb.Length-1,1);
} byte[] data = encoding.GetBytes(sb.ToString()); // 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// 发送数据
newStream.Write(data,0,data.Length);
newStream.Close();
}
我想是不是我传的参数不对,我是读了页面的html原码从中找到好多form
把里面的parameter找出来,有ActionMethod什么的,我不知道像actionName这样的参数是不是都要穿过去,传的方式是不是不一样?以下是html里的有关提交的js 和 htmlfunction postLinkChangeAction( formNameS, actionNameS, actionMethodS , action, func) {
x = false;
var formC = eval("document." + formNameS);
var tempAction = formC.action;
if (ClickFlgN == 1)
{
return false;
}
ClickFlgN = 1;
formC.action = action;
formC.actionNameS.value = actionNameS;
formC.actionMethodS.value = actionMethodS;
formC.currentFuncS.value = func;
formC.submit(); return false;
}--------------------------------------------------------<a href="" onClick="JavaScript:postLinkChangeAction('arrNormalConditionSearch','ArrNormalConditionSearchAction','searchApartment','http://xxx.xxx/Servlet','arrNormalSearch')
--------------
我认为是可以的,但是我不不知道怎么做。
帮你顶
这是我的问题,谁帮忙去看看
我想是不是我传的参数不对,我是读了页面的html原码从中找到好多form
把里面的parameter找出来,有ActionMethod什么的,我不知道像actionName这样的参数是不是都要穿过去,传的方式是不是不一样?以下是html里的有关提交的js 和 htmlfunction postLinkChangeAction( formNameS, actionNameS, actionMethodS , action, func) {
x = false;
var formC = eval("document." + formNameS);
var tempAction = formC.action;
if (ClickFlgN == 1)
{
return false;
}
ClickFlgN = 1;
formC.action = action;
formC.actionNameS.value = actionNameS;
formC.actionMethodS.value = actionMethodS;
formC.currentFuncS.value = func;
formC.submit(); return false;
}--------------------------------------------------------<a href="" onClick="JavaScript:postLinkChangeAction('arrNormalConditionSearch','ArrNormalConditionSearchAction','searchApartment','http://xxx.xxx/Servlet','arrNormalSearch')
formC.actionNameS.value = actionNameS;
formC.actionMethodS.value = actionMethodS;
formC.currentFuncS.value = func;<a href="" onClick="JavaScript:postLinkChangeAction('arrNormalConditionSearch','ArrNormalConditionSearchAction','searchApartment','http://xxx.xxx/Servlet','arrNormalSearch')
===========================================照这个JS来看,form发送数据好像是这样
url = "http://xxx.xxx/Servlet";
参数为string content =
@"actionNameS=ArrNormalConditionSearchAction&actionMethodS=searchApartment¤tFuncS=arrNormalSearch";你可以再试试看
我现在代码就是那样写的,且参数传的也是那几个
结果返回超时的叶面 不知道是不是跟session还有关系?
System.Net.HttpWebRequest request = ( System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.baidu.com");
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
Console.WriteLine(sr.ReadToEnd());
问题是servlet不是我做的,我现在琢磨出来可能是我去post的时候没有上一次会话的原因
可能跟cookies有关 。 不知道我怎么得到servlet服务器上的一个sessionID 并把它写的请求的cookie里
servlet服务端不是有每一次访问的sessionID么,不知道response里有没有带着sessionID一起返回来,有的话在哪里,得到这个sessionID写到下一次request的cookies里?
URL我写成http://www.xxxxx/servlet?参数名=参数
那些参数名是从网页原码里的form里找出来的
=================================================
不可以直接把参数写在url里面,要把url和参数分开写。如果你已经是分开写了,那继续……使用HttpWebRequest是单向后台发送POST的,如果它还要求Session,就比较麻烦了,考虑没有Session的情况,先试试看仿写一个js发送。private void PostForm(string formNameS, string actionNameS, string actionMethodS , action, string func)
{
StringBuilder sb = new StringBuilder();
sb.Append(@"<form id="""+formNameS+@""" action="""+action+@""" method=""post"">");
sb.Append(@"<input type=""hidden"" name=""actionName"" value="""+actionNameS+@"""/>");
sb.Append(@"<input type=""hidden"" name=""actionMethods"" value="""+actionMethodsS+@"""/>");
sb.Append(@"<input type=""hidden"" name=""currentFuncS"" value="""+func+@"""/>");
sb.Append(@"</form>");
sb.Append(@"<script language=""JAVAscript"">");
sb.Append(@"document.getElementById('"+formNameS+@"').submit();");
sb.Append(@"</script>");
Response.Write(sb.ToString());
}
用了上面的代码try了一下 结果还是返回超时叶面,证明服务端要求session
该如何解决呢
服务端要求请求必须有jsessionID这么个cookie
有没有人遇到过这个问题 如何解决
你如果提交异常的话应当检验你提交的数据。
更详细的参考内容请访问:http://blog.csdn.net/platform中关于httpwebrequest中的专题系列。
请求-------------> 从请求中读取Cookie--> 利用HttpWebRequest.CookieContainer将Cookie加入
回复<------------- 设置回复的Cookie <--- 利用HttpWebResponse.Cookies获得后台回复的Cookie集合
请求-------------> 从请求中读取Cookie--> 利用HttpWebRequest.CookieContainer将Cookie加入
回复<------------- 设置回复的Cookie <--- 利用HttpWebResponse.Cookies获得后台回复的Cookie集合
================================================================================
流程没大看懂 我现在的code这样写的private string post(string url,string[] param,string[] values)
{
string file_str = "";
string postData = "";
for(int i=0;i<param.Length;i++)
{
if(i != 0)
{
postData += "&"+param[i]+"="+values[i];
}
else
{
postData += param[i]+"="+values[i];
}
} byte[] data = Encoding.GetEncoding("SHIFT_JIS").GetBytes(postData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
if(cookieContainer == null)
{
cookieContainer = new CookieContainer();
}
request.CookieContainer = cookieContainer;
if(cookies != null)
{
for(int i=0;i<cookies.Count;i++)
{
request.CookieContainer.Add(cookies[i]);
}
}
request.Timeout = 600000;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.2; .NET CLR 1.1.4322)"; Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close(); HttpWebResponse hres = (HttpWebResponse)request.GetResponse();
cookieContainer = request.CookieContainer;
cookies = hres.Cookies;
using(StreamReader sr = new StreamReader(hres.GetResponseStream(), Encoding.GetEncoding("SHIFT_JIS")))
{
file_str = sr.ReadToEnd();
sr.Close();
}
return file_str;
}这个函数被反复调用,每一次响应的cookies都存在一个全局变量 cookies 里
不知道你说的流程是不是这个 但是运行的结果还是超时
另外,我在调试的时候观察响应的cookies这个属性里的内容没发现有jsessionid字样的东西
谢谢
这句话是什么意思啊?????
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=725879&SiteID=1
Cookies.Clear();
foreach (Cookie c in Request.Cookies)
{
Cookies.Add(c);
}...
// 将这个全局变量的Cookie加入到提交的申请
request.CookieContainer.Clear();
foreach (Cookie c in Cookies)
{
request.CookieContainer.Add(c)
}...
// 得到后台回复的时候,加入全局变量
HttpWebResponse hres = (HttpWebResponse)request.GetResponse();
Cookies.Clear();
foreach (Cookie c in hres.Cookies)
{
Cookies.Add(c);
}...
// 将全局变量写入回复用户的Cookie,准备下次使用
foreach (Cookie c in Cookies)
{
Response.Cookies.Add(c)
}
我按照上面的作了,仍然无效,返回超时叶面下面是我用工具抓得包---------------------------------------------------------------------------------
ie抓包(request方)(Request-Line):POST /Sumai/Servlet/SumaiCommon HTTP/1.1
Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Referer:http://sumai.ur-net.go.jp/Sumai/Servlet/SumaiCommon
Accept-Language:ja
Content-Type:application/x-www-form-urlencoded
Accept-Encoding:gzip, deflate
User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Host:sumai.ur-net.go.jp
Content-Length:406
Connection:Keep-Alive
Cache-Control:no-cache
Cookie:JSESSIONID=0ac8961a30d55cb2656163e647ff9406a81370d03d31.e38Ma3mLaNeSci0OchaPbxeRahz0iRjHmky; BIGipServerpool_int_sumai_http_8010=43436042.18975.0000
----------------------------------------------------------------------------
--------------------------------------------------------------------------
我的程序运行时抓得包 (request方)(Request-Line):POST /Sumai/Servlet/SumaiCommon HTTP/1.1
Accept-Language:ja
Accept-Encoding:gzip, deflate
Cache-Control:no-cache
Content-Type:application/x-www-form-urlencoded
User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
Content-Length:390
Expect:100-continue
Host:sumai.ur-net.go.jp
Cookie:JSESSIONID=0ac8961a30d535e3f631aedf4a70be8b891ec45198c0.e38Ma3mLaNeSci0OchaPbxeRahz0iRjHmky; BIGipServerpool_int_sumai_http_8010=26658826.18975.0000
------------------------------------------------------------------------由于程序第一次请求不会有jsessionid,第一次请求完以后返回给一个jsessionid,我把它保存下来用于第二次请求 所以请求了两次 上面是第二次请求的包
程序post的参数和ie post的比较了是一样的
用户请求
CookieContainer ServletCookies = new CookieContainer();
if (Session["ServletCookies"]!=null)
{
ServletCookies = (CookieContainer)Session["ServletCookies"];
}...
请求Server
request.CookieContainer = ServletCookies; // 我们刚才定义的
request.KeepAlive = true; // 保持一个永久连接...
得到回复...
回复用户
Session["ServletCookies"] = ServletCookies; // 结束前先把这个存起来。=======================
注:使用HttpWebRequest的时候,每次Create相当于一个新的请求。就好像每一次打开浏览器,打开网页,然后关闭了浏览器(这时候Session已经中断),下一次请求的时候,又重新开一个新的浏览器。我把这个CookieContainer保留在Session里面,不知道可以不可以,不可以再想办法。
string url = urlList[theUrlID];
this.textBox1.Text = url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
string url_ID = IDList[id];
this.textBox1.Text += "/n";
this.textBox1.Text += url_ID;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
System.Random Rand = new Random(); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8);//读取获得内容流
string content = reader.ReadToEnd();
if ( content.IndexOf("免责声明") > 0 )
{
this.richTextBox1.Text = content;
content = content.Replace("'", "");
string sql = "update comment set [HtmlInfo]='{0}',tag=1 where ID=" +url_ID ;
sql = string.Format(sql,content);
this.richTextBox1.Text = sql;
this.richTextBox1.ScrollToCaret();
SqlHelper.ExecuteNonQuery(cn, CommandType.Text, sql);
theUrlID++;
id++;
tryTimes = 1;
}
else
{
this.richTextBox1.Text = "尝试" + tryTimes + "次";
tryTimes++;
}
this.listBox1.SelectedIndex = theUrlID;