c# httppost方法
public struct PostFile
{
public string name;
public string filename;
public Stream bitmapStream;
}
public string PostStringFormPic(Dictionary<string, string> paramData, PostFile postfile, Encoding EncodingPostData = null, Encoding EncodingReadStream = null)
{
webRequest.Method = "POST";
//string ret = string.Empty;
Stream newStream = null;
//StreamReader sr = null;
if (EncodingReadStream == null)
{
EncodingReadStream = Encoding.UTF8;
}
if (EncodingPostData == null)
{
EncodingPostData = Encoding.UTF8;
}
try
{
//EncodingPostData = Encoding.GetEncoding("GB2312");
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 边界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
// 最后的结束符
var endBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
var memStream = new MemoryStream();
memStream.Write(beginBoundary, 0, beginBoundary.Length);
var header = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: image/jpeg\r\n\r\n";
header = string.Format(header, postfile.name, postfile.filename);
var headerbytes = EncodingPostData.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
postfile.bitmapStream.CopyTo(memStream);
var stringKeyHeader = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}";
// var rn = "\r\n";//换行
// var rnlength = rn.Length;
foreach (var item in paramData)
{
string data = string.Format(stringKeyHeader, item.Key, item.Value);
byte[] bdata = EncodingPostData.GetBytes(data);
memStream.Write(bdata, 0, bdata.Length);
//memStream.Write(EncodingPostData.GetBytes(rn), 0, rnlength);
}
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = memStream.Length;
//byte[] byteArray = EncodingPostData.GetBytes(paramData); //转化
// byte[] byteArray = Encoding.ASCII.GetBytes(paramData);
// webRequest.ContentLength = byteArray.Length;
ServicePointManager.Expect100Continue = false;
newStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
newStream.Write(tempBuffer, 0, tempBuffer.Length);//写入参数
newStream.Close();
//webResponse = (HttpWebResponse)webRequest.GetResponse();
////CheckProxy();
//sr = new StreamReader(webResponse.GetResponseStream(), EncodingReadStream);
//ret = sr.ReadToEnd();
//return ret;
string result = string.Empty;
webResponse = (HttpWebResponse)webRequest.GetResponse();
CheckProxy();
if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
{
using (System.IO.Stream stream = webResponse.GetResponseStream())
{
using (var zipStream =
new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(zipStream, EncodingReadStream))
{
result = sr.ReadToEnd();
}
}
}
}
else
{
using (System.IO.Stream stream = webResponse.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(stream, EncodingReadStream))
{
result = sr.ReadToEnd();
}
}
}
return result;
}
finally
{
if (WebResponse != null)
WebResponse.Close();
if (newStream != null)
newStream.Close();
}
}
php端接收 laravel框架
public function upload_headimg(Request $request)
{
//解析回传数据
$extend_data = json_decode(urldecode($request->input('extend_data', '')));
try {
$wxnum_model = new Models\Wxnum();
//微信公众号信息
$wxnum_info = $wxnum_model->select_wxnuminfo($extend_data->task_id);
$pb_headimg = array();
if ($request->hasFile('fileName')) {
$pb_headimg = $wxnum_model->update_headimg($wxnum_info, $request->file()['fileName']->getRealPath());
}
if (count($pb_headimg) > 0) Models\Wxnum::where('id', $wxnum_info->id)->update($pb_headimg);
return Models\ResponseRet::return_msg_success();
} catch (\Exception $e) {
Log::info('上传公众号头像:', $request->all());
Log::error(sprintf("%s 上传公众号头像失败 %s", $request->input('u_name', ''), $e->getMessage()));
return Models\ResponseRet::return_msg('1013');
}
}
|
请发表评论