c# httppost方法php
public struct PostFile
{
public string name;
public string filename;
public Stream bitmapStream;
}laravel
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;web
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框架json
public function upload_headimg(Request $request)
{
//解析回傳數據
$extend_data = json_decode(urldecode($request->input('extend_data', '')));c#
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'); } }