C#用HttpSendRequest如何post数据?

石之审判 2015-08-10 09:26:45
用winInet.dll里面的HttpSendRequest方法post数据,不管怎么设置参数,始终没有办法post上去,有没有遇到的?
//声明:
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
public static extern IntPtr HttpOpenRequest(IntPtr hConnect, string szVerb, string szURI, string szHttpVersion, string szReferer, StringBuilder accetpType, uint dwflags, int dwcontext);
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
public static extern bool HttpSendRequest(IntPtr hRequest, string szHeaders, long headersLen, byte[] data, long dataLen);


//调用时:
public const uint INTERNET_COOKIE_THIRD_PARTY = 0x10;
public const uint INTERNET_FLAG_RELOAD = 0x80000000;
uint flag = INTERNET_FLAG_RELOAD | INTERNET_COOKIE_THIRD_PARTY;
HTTP_VERSION = "HTTP/1.1";
IntPtr hRequest = HttpOpenRequest(hConnect, getOrPost.ToString(), uri.PathAndQuery, HTTP_VERSION, null, null, flag, 0);
bool sendResult = HttpSendRequest(hRequest, header, header.Length, post, post.Length);

其中,hConnect是上面方法得到的,保证没问题的,
getOrPost.ToString()="POST" //这个是枚举enum,不用考虑错误,
uri为链接,http或https开头,也没问题,至于协议头,也没问题,我用GET试过的
现在问题就在最后两个参数post和post.Length了
这个post,不管设置为什么类型(string、byte[]、StringBuilder)都不行啊,抓包发现,post数据为空。
我写的post是 "q=1&e=2"
格式没错,虽然数据可能不对,但是抓包不应该没有啊。
有没有用过这个的帮帮忙吧
...全文
304 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
石之审判 2015-08-10
  • 打赏
  • 举报
回复
引用 10 楼 shingoscar 的回复:
[quote=引用 9 楼 A12116 的回复:] [quote=引用 8 楼 shingoscar 的回复:] 服务器也没错误么? 错误日志全开了么? 有可能是数据格式不对,要不你贴的完整点看看
服务器?那只能问谷歌了啊。。。 现在的问题是post数据压根就没传上去,不知道这个类怎么回事,网上有人遇到过这个问题,但是没人回答啊[/quote] 你说的压根没传上去是怎么判断出来的?截包?[/quote] 对啊,用HttpAnalyzer抓包,发现post数据是空的
Poopaye 2015-08-10
  • 打赏
  • 举报
回复
引用 9 楼 A12116 的回复:
[quote=引用 8 楼 shingoscar 的回复:] 服务器也没错误么? 错误日志全开了么? 有可能是数据格式不对,要不你贴的完整点看看
服务器?那只能问谷歌了啊。。。 现在的问题是post数据压根就没传上去,不知道这个类怎么回事,网上有人遇到过这个问题,但是没人回答啊[/quote] 你说的压根没传上去是怎么判断出来的?截包?
石之审判 2015-08-10
  • 打赏
  • 举报
回复
引用 8 楼 shingoscar 的回复:
服务器也没错误么? 错误日志全开了么? 有可能是数据格式不对,要不你贴的完整点看看
服务器?那只能问谷歌了啊。。。 现在的问题是post数据压根就没传上去,不知道这个类怎么回事,网上有人遇到过这个问题,但是没人回答啊
Poopaye 2015-08-10
  • 打赏
  • 举报
回复
服务器也没错误么? 错误日志全开了么? 有可能是数据格式不对,要不你贴的完整点看看
石之审判 2015-08-10
  • 打赏
  • 举报
回复
引用 6 楼 shingoscar 的回复:
既然你一定要用,那就继续用GetLastError去查问题呗?
问题是没问题啊!! 所有返回值都没问题,单就是没有post数据! error一直是0,要是能查,我早就查了
Poopaye 2015-08-10
  • 打赏
  • 举报
回复
引用 5 楼 A12116 的回复:
我要的是HttpSendRequest,别给我别的啊!! 什么webRequest的我会!!! 别问我为啥一定要用这个,我有我的原因!
既然你一定要用,那就继续用GetLastError去查问题呗?
石之审判 2015-08-10
  • 打赏
  • 举报
回复
我要的是HttpSendRequest,别给我别的啊!! 什么webRequest的我会!!! 别问我为啥一定要用这个,我有我的原因!
宝_爸 2015-08-10
  • 打赏
  • 举报
回复
.net基础类库中有HttpWebRequest https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx 这里有个例子:

private static void start_post()
{
    //Our postvars
    byte[]  buffer = Encoding.ASCII.GetBytes( "test=postvar&test2=another" );
    //Initialization, we use localhost, change if applicable
    HttpWebRequest WebReq = 
	(HttpWebRequest)WebRequest.Create("http://127.0.0.1/test.php");
    //Our method is post, otherwise the buffer (postvars) would be useless
    WebReq.Method = "POST";
    //We use form contentType, for the postvars.
    WebReq.ContentType ="application/x-www-form-urlencoded";
    //The length of the buffer (postvars) is used as contentlength.
    WebReq.ContentLength = buffer.Length;
    //We open a stream for writing the postvars
    Stream PostData = WebReq.GetRequestStream();
    //Now we write, and afterwards, we close. Closing is always important!
    PostData.Write(buffer, 0, buffer.Length);
    PostData.Close();
    //Get the response handle, we have no true response yet!
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    //Let's show some information about the response
    Console.WriteLine(WebResp.StatusCode);
    Console.WriteLine(WebResp.Server);
           
    //Now, we read the response (the string), and output it.
    Stream Answer = WebResp.GetResponseStream();
    StreamReader _Answer = new StreamReader(Answer);
    Console.WriteLine(_Answer.ReadToEnd());

    //Congratulations, you just requested your first POST page, you
    //can now start logging into most login forms, with your application
    //Or other examples.
}
来自http://www.codeproject.com/Articles/18034/HttpWebRequest-Response-in-a-Nutshell-Part
宝_爸 2015-08-10
  • 打赏
  • 举报
回复
.net基础类库中有HttpWebRequest
Forty2 2015-08-10
  • 打赏
  • 举报
回复
为什么要那么麻烦的去用PInvoke,而不简单的用C#的WebHttpRequest呢?
石之审判 2015-08-10
  • 打赏
  • 举报
回复
没人知道?????

110,538

社区成员

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

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

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