C#妯℃��http ����post��get璇锋� ������

private string HttpPost( stringUrl,string postDataStr)

{

HttpWebRequest �request = (HttpWebRequest)WebRequest.Create(Url);

request.Method ="POST";

request.ContentType ="application/x-www-form-urlencoded";

request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);

request.CookieContainer = cookie;

Stream myRequestStream = request.GetRequestStream();

StreamWriter myStreamWriter =newStreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));

myStreamWriter.Write(postDataStr);

myStreamWriter.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

response.Cookies = cookie.GetCookies(response.ResponseUri);

Stream myResponseStream = response.GetResponseStream();

StreamReader myStreamReader =newStreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

stringretString = myStreamReader.ReadToEnd();

myStreamReader.Close();

myResponseStream.Close();

returnretString;

}

publicstringHttpGet(stringUrl,stringpostDataStr)

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr ==""?"":"?") + postDataStr);

request.Method ="GET";

request.ContentType ="text/html;charset=UTF-8";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream myResponseStream = response.GetResponseStream();

StreamReader myStreamReader =newStreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

stringretString = myStreamReader.ReadToEnd();

myStreamReader.Close();

myResponseStream.Close();

returnretString;

}

��post���跺�����朵��ㄧ����cookie锛����诲�163����浠舵�跺��灏遍��瑕�����cookie锛���浠ュ�ㄥ��ㄤ�涓�cookie灞��ч���朵�瀛� CookieContainer cookie = new CookieContainer();

锛�娉ㄦ��锛����跺��璇锋�浼���瀹���锛�浣���浠�灏遍��瑕�浠���瀹���url�峰��涓�瑗匡���QQ�诲��������峰��sid锛�浣�涓��㈢��浼����ㄦ�规����瀹����板��璺宠浆����浠���浠ョ��:

request.AllowAutoRedirect = false;璁剧疆��瀹���绂���锛�浣�灏卞��浠ヤ�headers��Location灞��т腑�峰����瀹����板��

你可能感兴趣的:(C#妯℃��http ����post��get璇锋� ������)