各位大神:
因为每天工作忙,想偷懒,所以自己写了一个模拟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分了,全给).