httpwebrequest访问.ashx

T_long 2011-01-10 05:19:53
用抓包工具获得出来的postdata

请问如何把postdata传入
...全文
579 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
T_long 2011-01-12
  • 打赏
  • 举报
回复
谢谢了,加分!!!
T_long 2011-01-12
  • 打赏
  • 举报
回复
那个参数我注意到了
好像我的
myRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
写的有问题
不管postdata传什么返回的都是
new Ajax.Web.DataSet([new Ajax.Web.DataTable([["wymc","System.String"],["wydz","System.String"]],[])]);/*
孟子E章 2011-01-12
  • 打赏
  • 举报
回复
string postData = "{\"wymc\":\"金\"}";
返回的内容

new Ajax.Web.DataSet([new Ajax.Web.DataTable([["wymc","System.String"],["wydz","System.String"]],[["金辉宾馆","金辉宾馆"],["金辉宾馆","兴工街沈辽路"],["金茂国际公寓","中街新玛特对面,苏宁电器楼上"],["宏伟金都","建设大路62号"],[" 金辉宾馆","兴工街沈辽路"],["金居花园","兴工街沈辽路"],["金辉佳苑","云峰街沈辽路"],["金华苑","东陵区方家栏"],["中房金荷苑","浑南彩霞街"],["中房·金河花园","沈阳市浑南新区彩霞街36号"]])]);
孟子E章 2011-01-12
  • 打赏
  • 举报
回复
下面的代码测试通过,返回的内容是

new Ajax.Web.DataSet([new Ajax.Web.DataTable([["wymc","System.String"],["wydz","System.String"]],[["126附近","和平"],["加洲阳光花园别墅,6房低价急租同学朋友合租,办公仓库首选","于洪区怒江北街与白山路交汇处,北陵西门."],["昆山小区(四院附近)226号 ","昆山小区(四院附近)226号 "],["出租 大东兴隆新光店对面单间 1600一季","八家子"],["御龙逸城186","东陵区方明街"],["领事馆附近,6楼,单间,700元","三经街,领事馆"],["铁西滑翔6小区","铁西滑翔6小区"],["和平区126中学附近","沈阳市和平区八纬路"],["保利花园三期双河城36#1-2-3","东陵区东安路78号"],["三好街艾特花园6号楼","三好街"]])]);/*


代码
Encoding encoding = Encoding.GetEncoding("utf-8");
string postData = "{\"wymc\":\"6\"}";
string strUrl = "http://esf.bxgfw.com/ajaxpro/user_admin_renthouse_pub,App_Web__nxws2da.ashx?";
byte[] data = encoding.GetBytes(postData);
// 准备请求... {"mcstr":"辽中"}
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method = "POST";
myRequest.Referer = "http://esf.bxgfw.com/shop/admin/renthouse_pub.aspx";
myRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myRequest.Headers.Add("X-AjaxPro-Method", "getSearchList");
myRequest.ContentType = "application/x-www-form-urlencoded";

//注意这里的Cookie进行更换成你的登录信息
myRequest.Headers.Add("Cookie", "ASP.NET_SessionId=42jidiukrk5d1cugei0bhsup; cnzz_a2178262=6; sin2178262=; rtime=0; ltime=1294802416124; cnzz_eid=41664513-1294801319-http%3A//esf.bxgfw.com/shop/admin/renthouse_pub.aspx; lzstat_uv=38694578793119418740|1524099; lzstat_ss=3351439025_2_1294830879_1524099; user=hyloginstate=success!&hybh=G11011211150315&hylx=%b8%f6%c8%cb%bb%e1%d4%b1&hyzh=net_lover&hyyj=&hyjb=%c6%d5%cd%a8%bb%e1%d4%b1&hyjf=0&hysftgyz=1&hymc=%c0%ed%cf%eb&hylxdh=13910309166&hylxr=net_lover");
myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// 发送数据
newStream.Write(data, 0, data.Length);
newStream.Close();

//得到服务器端的返回
HttpWebResponse res = myRequest.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.GetEncoding("utf-8"));
Response.Write(sr.ReadToEnd());
sr.Close();
res.Close();


T_long 2011-01-12
  • 打赏
  • 举报
回复
直接访问这个页面http://esf.bxgfw.com/ajaxpro/user_admin_renthouse_pub,App_Web__nxws2da.ashx
源码为
if(typeof user_admin_renthouse_pub == "undefined") user_admin_renthouse_pub={};
user_admin_renthouse_pub_class = function() {};
Object.extend(user_admin_renthouse_pub_class.prototype, Object.extend(new AjaxPro.AjaxClass(), {
Getqxsq: function(mcstr) {
return this.invoke("Getqxsq", {"mcstr":mcstr}, this.Getqxsq.getArguments().slice(1));
},
getSearchList: function(wymc) {
return this.invoke("getSearchList", {"wymc":wymc}, this.getSearchList.getArguments().slice(1));
},
getSearchListResult: function(wymc) {
return this.invoke("getSearchListResult", {"wymc":wymc}, this.getSearchListResult.getArguments().slice(1));
},
url: '/ajaxpro/user_admin_renthouse_pub,App_Web__nxws2da.ashx'
}));
user_admin_renthouse_pub = new user_admin_renthouse_pub_class();

T_long 2011-01-12
  • 打赏
  • 举报
回复
不是自己写的,那些属性我都从stream上面复制过去的
T_long 2011-01-12
  • 打赏
  • 举报
回复
改了,返回的成
null; r.error = {"Message":"word C","Type":"System.NotSupportedException"};/*
孟子E章 2011-01-12
  • 打赏
  • 举报
回复
你提交的页面是你自己写的吗?不管是不是你自己的,你需要清楚那个页面需要哪些信息以及格式是什么
孟子E章 2011-01-12
  • 打赏
  • 举报
回复
人家需要参数Content
byte[] byteRequest = "Content=" + Encoding.Default.GetBytes("{'wymc':'" + System.Web.HttpUtility.UrlEncode("金", Encoding.GetEncoding("utf-8"))+"'}");


T_long 2011-01-12
  • 打赏
  • 举报
回复
也就是这个
string accept = "*/*";

string userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";

Encoding encoding = Encoding.GetEncoding("utf-8");
CookieContainer cc = PublishVoid.htLogin[LoginUrl] as CookieContainer;
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
byte[] byteRequest = Encoding.Default.GetBytes("{'wymc':'" + System.Web.HttpUtility.UrlEncode("金", Encoding.GetEncoding("utf-8"))+"'}");
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(GetXiaoQuUrl);
httpWebRequest.CookieContainer = cc;
httpWebRequest.ContentType = "text/plain; charset=utf-8";
httpWebRequest.Referer = "http://esf.bxgfw.com/shop/admin/renthouse_pub.aspx";
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = byteRequest.Length;
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.Headers.Add("x-ajaxpro-method", "getSearchList");
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
httpWebResponse.Cookies = cc.GetCookies(httpWebRequest.RequestUri);
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding);

string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();

加了httpWebRequest.Headers.Add("x-ajaxpro-method", "getSearchList");
现在html返回null; r.error = {"Message":"The string could not be read.","Type":"System.NotSupportedException"};/*
T_long 2011-01-12
  • 打赏
  • 举报
回复
 byte[] byteRequest = Encoding.Default.GetBytes(postData);
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
httpWebRequest.Referer = url;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = isPost ? "POST" : "GET";
httpWebRequest.ContentLength = byteRequest.Length;
httpWebRequest.AllowAutoRedirect = true;
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
httpWebResponse.Cookies = cookieContainer.GetCookies(httpWebRequest.RequestUri);
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding);
//cc.SetCookies(httpWebRequest.RequestUri);
cc = cookieContainer;
if (!(PublishVoid.htLogin.Contains(url)))
PublishVoid.htLogin.Add(url, cc);
string html = streamReader.ReadToEnd();

我访问其他的页面就能正常返回,这个返回的html就是空
那个参数也已经转换了
System.Web.HttpUtility.UrlEncode("金", Encoding.GetEncoding("utf-8"))
孟子E章 2011-01-12
  • 打赏
  • 举报
回复
注意编码需要更改下,与web,config里面的编码一致,就不会乱码了
孟子E章 2011-01-12
  • 打赏
  • 举报
回复
没有区别啊,
完整的代码

Encoding encoding = Encoding.GetEncoding("utf-8");
string postData = "{'A':'测试'}";
string strUrl = "http://localhost:57606/WebSite1/HttpReceiveData.ashx";
byte[] data = encoding.GetBytes(postData);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
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();

//得到服务器端的返回
HttpWebResponse res = myRequest.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.GetEncoding("utf-8"));
Response.Write(sr.ReadToEnd());//或者MessageBox.Show(sr.ReadToEnd());
sr.Close();
res.Close();



HttpReceiveData.ashx代码
<%@ WebHandler Language="C#" Class="HttpReceiveData" %>

using System;
using System.Web;

public class HttpReceiveData : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream, System.Text.Encoding.GetEncoding("utf-8"));
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write("接收到的值是:" + sr.ReadToEnd());
sr.Close();
}

public bool IsReusable
{
get
{
return false;
}
}

}
T_long 2011-01-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 net_lover 的回复:]
C# code
Encoding encoding = Encoding.GetEncoding("GB2312");
string postData = "{'A':'测试'}";
string strUrl = "http://xxxxxxxxxxxxxxxxxxxxxx/HttpReceiveData.ashx";
byte[] data = encoding.GetBytes……
[/Quote]
开始就是这样写的,返回的就是空
我想知道这个跟其他的普通页面有区别吗?
孟子E章 2011-01-12
  • 打赏
  • 举报
回复
Encoding encoding = Encoding.GetEncoding("GB2312"); 
string postData = "{'A':'测试'}";
string strUrl = "http://xxxxxxxxxxxxxxxxxxxxxx/HttpReceiveData.ashx";
byte[] data = encoding.GetBytes(postData);
// 准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
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();
T_long 2011-01-12
  • 打赏
  • 举报
回复
获得的stream是这个样子
POST /ajaxpro/user_admin_renthouse_pub,App_Web__nxws2da.ashx HTTP/1.1
Accept: */*
Accept-Language: zh-cn
x-ajaxpro-method: getSearchList
Referer: http://esf.bxgfw.com/shop/admin/renthouse_pub.aspx
Content-Type: text/plain; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Host: esf.bxgfw.com
Content-Length: 14
Connection: Keep-Alive
Cache-Control: no-cache

{"wymc":"涓?}

{"wymc":"涓?}
访问的页面是http://esf.bxgfw.com/ajaxpro/user_admin_renthouse_pub,App_Web__nxws2da.ashx
T_long 2011-01-12
  • 打赏
  • 举报
回复
Encoding.Default.GetBytes("{\"wymc\":\"金\"}");



Encoding encoding = Encoding.GetEncoding("utf-8");
byte[] data = encoding.GetBytes(postData);
这两句话也有区别…………
孟子E章 2011-01-10
  • 打赏
  • 举报
回复
请问如何把postdata传入哪里?
"a=xxxxx"

接收Request.Form["a"]
netharry 2011-01-10
  • 打赏
  • 举报
回复
和aspx一样访问,只是地址是变成http.....ashx

110,536

社区成员

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

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

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