If you want to reprint it, Please retain your copyright:
/*
* Description: in ASP. NETCodeAt the same time, post the common form field and file form field data to the specified server URL.
* Auther: chongchong2008-innocent blue
* MSN: chongchong2008@msn.com
* QQ: 154674958
* Blog: chongchong2008.cnblogs.com
* Dates: 2012-02-28
* Copyright: chongchong2008 Yichang Hubei China
*/
Using system;
Using system. collections;
Using system. Collections. Generic;
Using system. LINQ;
Using system. IO;
Using system. text;
Using system. net;
namespace httprequest
{< br> ///
// Description: http post request client
/// Author: chongchong2008
// last-modified-Date:
//
public class httprequestclient
{< br> # region // field
private arraylist bytesarray;
private encoding = encoding. utf8;
private string boundary = string. empty;
# endregion
# region // constructor
Public httprequestclient ()
{< br> bytesarray = new arraylist ();
string flag = datetime. now. ticks. tostring ("X");
boundary = "-------------------------" + flag;
}< BR ># endregion
# region // method
///
// merge request data
///
/ //
private byte [] mergecontent ()
{< br> int length = 0;
int readlength = 0;
string endboundary = "--" + boundary + "-- \ r \ n ";
byte [] endboundarybytes = encoding. getbytes (endboundary);
Bytesarray. Add (endboundarybytes );
Foreach (byte [] B in bytesarray)
{
Length + = B. length;
}
Byte [] bytes = new byte [length];
Foreach (byte [] B in bytesarray)
{
B. copyto (bytes, readlength );
Readlength + = B. length;
}
Return bytes;
}
///
/// upload
///
/// request URL
/// response
///
Public bool upload (string requesturl, out string responsetext)
{< br> WebClient = new WebClient ();
WebClient. headers. add ("Content-Type", "multipart/form-data; boundary =" + boundary);
Byte [] responsebytes;
Byte [] bytes = mergecontent ();
Try
{
Responsebytes = WebClient. uploaddata (requesturl, bytes );
Responsetext = system. Text. encoding. utf8.getstring (responsebytes );
Return true;
}
Catch (webexception ex)
{
Stream responsestream = ex. response. getresponsestream ();
Responsebytes = new byte [ex. response. contentlength];
Responsestream. Read (responsebytes, 0, responsebytes. Length );
}
Responsetext = system. Text. encoding. utf8.getstring (responsebytes );
Return false;
}
///
// set the form data field
///
/// Field name
// field value
//
Public void setfieldvalue (string fieldname, string fieldvalue)
{< br> string httprow = "--" + boundary + "\ r \ ncontent-Disposition: Form-data; name = \ "{0} \" \ r \ n {1} \ r \ n ";
string httprowdata = string. format (httprow, fieldname, fieldvalue);
Bytesarray. Add (encoding. getbytes (httprowdata ));
}
/// <Summary>
/// Set the Form file data
/// </Summary>
/// <Param name = "fieldname"> Field name </param>
/// <Param name = "FILENAME"> field value </param>
/// <Param name = "contenttype"> content type </param>
/// <Param name = "filebytes"> file byte stream </param>
/// <Returns> </returns>
Public void setfieldvalue (string fieldname, string filename, string contenttype, byte [] filebytes)
{
String end = "\ r \ n ";
String httprow = "--" + boundary + "\ r \ ncontent-Disposition: Form-data; name = \" {0 }\"; filename = \ "{1} \" \ r \ ncontent-type: {2} \ r \ n ";
String httprowdata = string. Format (httprow, fieldname, filename, contenttype );
Byte [] headerbytes = encoding. getbytes (httprowdata );
Byte [] endbytes = encoding. getbytes (end );
Byte [] filedatabytes = new byte [headerbytes. Length + filebytes. Length + endbytes. Length];
Headerbytes. copyto (filedatabytes, 0 );
Filebytes. copyto (filedatabytes, headerbytes. Length );
Endbytes. copyto (filedatabytes, headerbytes. Length + filebytes. Length );
Bytesarray. Add (filedatabytes );
}
# Endregion
}
}
Usage:
Bytes ----------------------------------------------------------------------------------------------------------------
String url = "http: // 127.0.0.1/test/upload"; // address of the page to be sent
String image_path = "C:/test.jpg ";
Filestream FS = new filestream (image_path, system. Io. filemode. Open, system. Io. fileaccess. Read );
Byte [] filebytes = new byte [fs. Length];
FS. Read (filebytes, 0, filebytes. Length );
FS. Close ();
FS. Dispose ();
Httprequestclient = new httprequestclient ();
Httprequestclient. setfieldvalue ("ID", "123 ");
Httprequestclient. setfieldvalue ("user_name", "chongchong2008 ");
Httprequestclient. setfieldvalue ("type_id", "1 ");
Httprequestclient. setfieldvalue ("uploadfile", "test.jpg", "application/octet-stream", filebytes );
String responsetext;
Bool uploaded = httprequestclient. Upload (URL, out responsetext );
========================================================== ==========================================
Referrer link: http://www.cnblogs.com/dotey/
========================================================== ==========================================