110,538
社区成员
发帖
与我相关
我的任务
分享
//声明:
[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);
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