求教 模拟post请求

yhwobxv2 2013-12-09 03:42:28
各位大神:
因为每天工作忙,想偷懒,所以自己写了一个模拟post请求(模拟一个网站的签名),
其中post请求的时候遇到了问题.

以下是我在firefox中捕获到的:
请求地址:
http://chengdu.safetree.com.cn/ajax/EPlatform.SafeSchool.StuSafeCourseText,EPlatform.ashx?_method=InsertCourseStatus&_session=rw
参数:

POST数据:

响应:



我自己的代码

string qianming = @"http://chengdu.safetree.com.cn/ajax/EPlatform.SafeSchool.StuSafeCourseText,EPlatform.ashx?_method=InsertCourseStatus&_session=rw";
string qianmingData = "CouserID=1058&GradeId=832&objName=CourseStatus&ObjType=1&ObjID=1058&ObName=安全用药常识";


string postData = PostData(qianming, qianmingData);

private string PostData(string url, string data)
{
string respHtml = "";
UTF8Encoding coding = new UTF8Encoding();
byte[] postData = coding.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Timeout = 4000;
request.KeepAlive = true;
request.ContentLength = postData.Length;
request.CookieContainer = loginCookie;

Stream newStream = request.GetRequestStream();

//发送数据
newStream.Write(postData, 0, postData.Length);
newStream.Close();


//接收返回值
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie cookie in response.Cookies)
{
loginCookie.Add(cookie);
}
Stream myStream = response.GetResponseStream();
StreamReader myReader = new StreamReader(myStream, Encoding.GetEncoding("utf-8"));
respHtml = myReader.ReadToEnd();

response.Close();
myStream.Close();

response = null;
myStream = null;
myReader = null;

return respHtml;
}
catch (Exception ex)
{
return respHtml=ex.ToString();
}
}


问题:请求地址中有2个参数 _method 和 _session,我如果把_去掉再做post请求的话,返回的是一段JS代码.
// cached javascript
var StuSafeCourseText = {
CourseGet:function(id,callback,context){return new ajax_request(this.url + '?_method=CourseGet&_session=rw','id=' + enc(id),callback, context);},
VedioListsGet:function(id,type,callback,context){return new ajax_request(this.url + '?_method=VedioListsGet&_session=rw','id=' + enc(id)+ '\r\ntype=' + enc(type),callback, context);},
InsertCourseStatus:function(CouserID,GradeId,objName,ObjType,ObjID,ObName,callback,context){return new ajax_request(this.url + '?_method=InsertCourseStatus&_session=rw','CouserID=' + enc(CouserID)+ '\r\nGradeId=' + enc(GradeId)+ '\r\nobjName=' + enc(objName)+ '\r\nObjType=' + enc(ObjType)+ '\r\nObjID=' + enc(ObjID)+ '\r\nObName=' + enc(ObName),callback, context);},
ClassAllGet:function(Grade,callback,context){return new ajax_request(this.url + '?_method=ClassAllGet&_session=rw','Grade=' + enc(Grade),callback, context);},
InsertFeeBack:function(sContent,phone,Orhelpme,CoruseName,callback,context){return new ajax_request(this.url + '?_method=InsertFeeBack&_session=rw','sContent=' + enc(sContent)+ '\r\nphone=' + enc(phone)+ '\r\nOrhelpme=' + enc(Orhelpme)+ '\r\nCoruseName=' + enc(CoruseName),callback, context);},
url:'/ajax/EPlatform.SafeSchool.StuSafeCourseText,EPlatform.ashx'
}
function HtmlControl(id) {
var ele = null;
if(typeof(id) == 'object') ele = id; else ele = document.getElementById(id);
if(ele == null) return null;
var _o = ele.cloneNode(true);
var _op = document.createElement('SPAN');
_op.appendChild(_o);
this._source = _op.innerHTML;
}
HtmlControl.prototype.toString = function(){ return this._source; }

function HtmlControlUpdate(func, parentId) {
var f,i,ff,fa='';
var ele = document.getElementById(parentId);
if(ele == null) return;
var args = [];
for(i=0; i<HtmlControlUpdate.arguments.length; i++)
args[args.length] = HtmlControlUpdate.arguments[i];
if(args.length > 2)
for(i=2; i<args.length; i++){fa += 'args[' + i + ']';if(i < args.length -1){ fa += ','; }}
f = '{"invoke":function(args){return ' + func + '(' + fa + ');}}';
ff = null;eval('ff=' + f + ';');
if(ff != null && typeof(ff.invoke) == 'function')
{
var res = ff.invoke(args);
if(res.error != null){alert(res.error);return;}
ele.innerHTML = res.value;
}
}
function AjaxImage(url){var img=new Image();img.src=url;return img;}
function digi(v, c){v = v + "";var n = "0000";if(v.length < c) return n.substr(0, c-v.length) + v;return v;}
function DateTime(year,month,day,hours,minutes,seconds){if(year>9999||year<1970||month<1||month>12||day<0||day>31||hours<0||hours>23||minutes<0||minutes>59||seconds<0||seconds>59)throw("ArgumentException");this.Year = year;this.Month = month;this.Day = day;this.Hours = hours;this.Minutes = minutes;this.Seconds = seconds;}
DateTime.prototype.toString = function(){return digi(this.Year,4) + digi(this.Month,2) + digi(this.Day,2) + digi(this.Hours,2) + digi(this.Minutes,2) + digi(this.Seconds,2);}
function _getTable(n,e){for(var i=0; i<e.Tables.length; i++){if(e.Tables[i].Name == n)return e.Tables[i];}return null;}
function TimeSpan(){this.Days=0;this.Hours=0;this.Minutes=0;this.Seconds=0;this.Milliseconds=0;}
TimeSpan.prototype.toString = function(){return this.Days+'.'+this.Hours+':'+this.Minutes+':'+this.Seconds+'.'+this.Milliseconds;}

如果不去掉返回的却是:new Object();r.error = new ajax_error('System.FormatException','输入字符串的格式不正确。\r\nCould not retreive parameters from HTTP request.',0)new Object();r.error = new ajax_error('System.ArgumentException','类型“System.DBNull”的对象无法转换为类型“System.Int32”。',0)

这2个返回都不是正确的话,请问各位大神如何修改?作为一个业余.net爱好者在此感激不尽(只有100分了,全给).
...全文
243 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_16390239 2014-06-10
  • 打赏
  • 举报
回复
引用 9 楼 yhwobxv2 的回复:
[quote=引用 8 楼 yhwobxv2 的回复:] [quote=引用 7 楼 feiyun0112 的回复:] InsertCourseStatus:function(CouserID,GradeId,objName,ObjType,ObjID,ObName,callback,context){return new ajax_request(this.url + '?_method=InsertCourseStatus&_session=rw','CouserID=' + enc(CouserID)+ '\r\nGradeId=' + enc(GradeId)+ '\r\nobjName=' + enc(objName)+ '\r\nObjType=' + enc(ObjType)+ '\r\nObjID=' + enc(ObjID)+ '\r\nObName=' + enc(ObName),callback, context);}, 看看原始信息,他发的参数好像不是用&,是用\r\n连接
我试试[/quote] 谢谢 大神 已正确返回 OK[/quote] 您好,可以教學一下嗎? 我也遇到這個問題了,我是用apache http的套件
yhwobxv2 2013-12-09
  • 打赏
  • 举报
回复
引用 8 楼 yhwobxv2 的回复:
[quote=引用 7 楼 feiyun0112 的回复:] InsertCourseStatus:function(CouserID,GradeId,objName,ObjType,ObjID,ObName,callback,context){return new ajax_request(this.url + '?_method=InsertCourseStatus&_session=rw','CouserID=' + enc(CouserID)+ '\r\nGradeId=' + enc(GradeId)+ '\r\nobjName=' + enc(objName)+ '\r\nObjType=' + enc(ObjType)+ '\r\nObjID=' + enc(ObjID)+ '\r\nObName=' + enc(ObName),callback, context);}, 看看原始信息,他发的参数好像不是用&,是用\r\n连接
我试试[/quote] 谢谢 大神 已正确返回 OK
yhwobxv2 2013-12-09
  • 打赏
  • 举报
回复
引用 7 楼 feiyun0112 的回复:
InsertCourseStatus:function(CouserID,GradeId,objName,ObjType,ObjID,ObName,callback,context){return new ajax_request(this.url + '?_method=InsertCourseStatus&_session=rw','CouserID=' + enc(CouserID)+ '\r\nGradeId=' + enc(GradeId)+ '\r\nobjName=' + enc(objName)+ '\r\nObjType=' + enc(ObjType)+ '\r\nObjID=' + enc(ObjID)+ '\r\nObName=' + enc(ObName),callback, context);}, 看看原始信息,他发的参数好像不是用&,是用\r\n连接
我试试
feiyun0112 2013-12-09
  • 打赏
  • 举报
回复
InsertCourseStatus:function(CouserID,GradeId,objName,ObjType,ObjID,ObName,callback,context){return new ajax_request(this.url + '?_method=InsertCourseStatus&_session=rw','CouserID=' + enc(CouserID)+ '\r\nGradeId=' + enc(GradeId)+ '\r\nobjName=' + enc(objName)+ '\r\nObjType=' + enc(ObjType)+ '\r\nObjID=' + enc(ObjID)+ '\r\nObName=' + enc(ObName),callback, context);},

看看原始信息,他发的参数好像不是用&,是用\r\n连接
yhwobxv2 2013-12-09
  • 打赏
  • 举报
回复
引用 5 楼 Lost_Painting 的回复:
从提示来看,第二个已经正确提交了. 但是由于提交的内容中存在问题或者缺少字段,造成 "类型“System.DBNull”的对象无法转换为类型“System.Int32”。"
对啊,我现在也困惑在这里 ,我GET其他页面都没问题,而且html解析我需要的内容我也做好了. 这个post我都弄了好几天了 还是没弄好 有没有大神能远程帮助我 我的QQ 35914052 在此谢过
鸭梨山大帝 2013-12-09
  • 打赏
  • 举报
回复
从提示来看,第二个已经正确提交了. 但是由于提交的内容中存在问题或者缺少字段,造成 "类型“System.DBNull”的对象无法转换为类型“System.Int32”。"
yhwobxv2 2013-12-09
  • 打赏
  • 举报
回复
引用 2 楼 feiyun0112 的回复:
是不是有登录操作,你看firefox,应该有cookie 你没传cookie ***************************************************************************** 签名档: http://feiyun0112.cnblogs.com/
有登陆操作 private string GetLoginData(string url) { string respHtml=null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent ="Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; //request.Referer = server; request.Timeout = 4000; request.KeepAlive = true; request.CookieContainer = loginCookie; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); foreach (Cookie cookie in response.Cookies) { loginCookie.Add(cookie); } StreamReader myReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); respHtml = myReader.ReadToEnd().Replace("\r\n",""); response.Close(); response = null; myReader = null; return respHtml; } catch (WebException ex) { return respHtml = ex.ToString(); } } 这个是登陆 把cookie存到一个容器里的 post请求的时候带上了这个容器 我在做其他get请求的时候 都能返回正确的html代码
水猿兵团五哥 2013-12-09
  • 打赏
  • 举报
回复
太长了。。。。看不懂
feiyun0112 2013-12-09
  • 打赏
  • 举报
回复
是不是有登录操作,你看firefox,应该有cookie

你没传cookie

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
yhwobxv2 2013-12-09
  • 打赏
  • 举报
回复
public static CookieContainer loginCookie = new CookieContainer(); 内容太长了,没人看么?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧