貼一下自己的源碼
post:
string postData = "username=" + username + "&password=" + password;
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 300000;
Encoding encoding = Encoding.GetEncoding("gb2312");//進行中文亂碼
byte[] bytes = encoding.GetBytes(postData);
request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
string responseString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
MessageBox.Show(responseString);
get:
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "http://www.google.com/webhp?hl=zh-CN" );
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
//在這裡對接收到的頁面内容進行處理
}
post上傳檔案
private void addpic_Click(object sender, EventArgs e)
this.openPic.Filter = "圖檔檔案(*.jpg,*.gif,*.bmp)|*.jpg|*.gif|*.bmp";
if(DialogResult.OK==this.openPic.ShowDialog()&&(this.openPic.FileName!=null))
Bitmap bit = new Bitmap(this.openPic.FileName);
this.pictureBox1.Image = Image.FromHbitmap(bit.GetHbitmap());
int last = this.openPic.FileName.LastIndexOf(".");
string dat = this.openPic.FileName.Substring(last);
DateTime date = DateTime.Now;
picname=date.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);
picname = picname + dat;
//MessageBox.Show(picpath + @"/" + picname + ".png");
//File.Move(this.openPic.FileName,picpath+@"/"+picname);
File.Copy(this.openPic.FileName, picpath + @"/" + picname);
public void UploadFileBinary1(string localFile, string uploadUrl)
try
DateTime time1;
time1 = DateTime.Now;
long num3 = time1.Ticks;
//text1 = "---------------------" + num3.ToString("x");
string boundary = "---------------------------" + num3.ToString("x");
// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("/r/n");
sb.Append("Content-Disposition: form-data; name=/"file/"; filename=/"");
sb.Append(localFile);
sb.Append("/"");
sb.Append("Content-Type: application/octet-stream");
// sb.Append("Content-Type: application/x-xml");
//sb.Append("Content-Type: text/xml");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("/r/n--" + boundary + "/r/n");
// Open the local file
FileStream rdr = new FileStream(localFile, FileMode.Open);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
req.Method = "post";
req.AllowWriteStreamBuffering = true;
req.ContentType = "multipart/form-data boundary=" + boundary;
//req.ContentLength == boundaryBytes.Length + postHeaderBytes.Length + rdr.Length;
req.ContentLength = boundaryBytes.Length + postHeaderBytes.Length + rdr.Length;
// Retrieve request stream
Stream reqStream = req.GetRequestStream();
// BinaryReader r = new BinaryReader(rdr);
// byte[] postArray = r.ReadBytes((int)fs.Length);
// Stream postStream = myWebClient.OpenWrite(uriString, "PUT");
reqStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// Allocate byte buffer to hold file contents
long length = rdr.Length;
byte[] inData = new byte[409600];
// loop through the local file reading each data block
// and writing to the request stream buffer
int bytesRead = rdr.Read(inData, 0, inData.Length);
while (bytesRead > 0)
reqStream.Write(inData, 0, bytesRead);
bytesRead = rdr.Read(inData, 0, inData.Length);
rdr.Close();
reqStream.Write(boundaryBytes, 0, boundaryBytes.Length);
reqStream.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
string retString = myStreamReader.ReadToEnd();
// response.Close();
catch (Exception ex)
string exContent;
exContent = ex.ToString();
MessageBox.Show(exContent);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";//這無論是賦post還是get,都必須用全大寫,此值錯一點,都會導緻程式錯誤,因為不符合http協定
request.ContentType = "multipart/form-data; boundary=--abc";//或者為"application/x-www-form-urlencoded",對應form标簽裡的 enctype屬性,boundary那部分查是FORM元素各值的分隔符,具體請查閱HTTP協定相關文檔,如果此值用application/x- www-form-urlencoded則form各元素用&來分隔,而元素的值是經過了url編碼,調用System.Web.HttpUtility.UrlEncode方法,就能将值進行url編碼。
//如果需要加cookie,則按如下方式添加,具體請參閱msdn
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Cookie("test", "i love u", "/", "localhost"));
byte[] data = Encoding.GetEncoding(encoding).GetBytes(postData);//将要發送的資料按HTTP協定拼裝好字元串轉成位元組數組
request.ContentLength = data.Length;//設定内容的長度,長度就是要發送的整個位元組數組的長度,此句必須有,長度不對就會導緻錯誤
request.GetRequestStream().Write(data, 0, data.Length);//擷取request的流,将資料寫入流中,至此完成了form送出的必須有的步驟
response = (HttpWebResponse)request.GetResponse();//最後取得response擷取伺服器端的傳回資料進行處理
本文轉自xyz_lmn51CTO部落格,原文連結:http://blog.51cto.com/xyzlmn/819087,如需轉載請自行聯系原作者