$fields['sdimagefile[0]'] = '@'.realpath('sites/all/modules/ppt_microsoft_office/xiaoyuan_test_temp_file/images_s/ppttest1_s.jpg');
$fields['sdimagefile[1]'] = '@'.realpath('sites/all/modules/ppt_microsoft_office/xiaoyuan_test_temp_file/images_s/ppttest2_s.jpg');
$fields['uid'] = 321;//文件的uid
$fields['ppt_title'] = '测试文件';//ppt文件标题
//脚本
$fields['ppt_script[0]'] ='asdfasdfa';
$fields['ppt_script[1]'] ='dujiahuidujihiasdufia';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://kaifa.huandengpai.com/pptology/pptms/image/createPPTMSImageNode");
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
$result = curl_exec($ch);
curl_close ($ch);
帮顶一下
lululululululullulululululululuaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
就是发送一个post请求 提交一些参数到一个url吧
C#模拟web请求常用webclient,httpwebrequest等实现
搜下吧 挺简单的
主要是数组上传。比如ppt_script这个name,有n个,还有图片文件也是n个,都是一个name叫sdimagefile
HttpRequestClient httpRequestClient = new HttpRequestClient();
httpRequestClient.SetFieldValue("ppt_uid", Globals.ThisAddIn.userId.ToString());
httpRequestClient.SetFieldValue("ppt_title", HttpUtil.UrlEncode(present.Name));
httpRequestClient.SetFieldValue("ppt_nid", Globals.ThisAddIn.ppt_nid.ToString());
httpRequestClient.SetFieldValue("source_nid", Globals.ThisAddIn.ppt_file_nid.ToString());
for (int i = 0; i < scripts.Count; i++)
{
httpRequestClient.SetFieldValue(string.Format("ppt_script[{0}]", i), scripts[i]);
}
for (int i = 0; i < hdimagefilesArray.Count; i++)
{
httpRequestClient.SetFieldValue(string.Format("hdimagefile[{0}]", i), Path.GetFileName(i + ".png"), "application/octet-stream", hdimagefilesArray[i]);
httpRequestClient.SetFieldValue(string.Format("sdimagefile[{0}]", i), Path.GetFileName(i + ".png"), "application/octet-stream", sdimagefilesArray[i]);
}
hdimagefilesArray.Clear();
sdimagefilesArray.Clear();
string responseText = "";
httpRequestClient.Upload(url, out responseText);
下面这个类从网上撸的。。
public class HttpRequestClient
{
#region //字段
private ArrayList bytesArray;
private Encoding encoding = Encoding.UTF8;
private string boundary = String.Empty;
#endregion
#region //构造方法
public HttpRequestClient()
{
bytesArray = new ArrayList();
string flag = DateTime.Now.Ticks.ToString("x");
boundary = "---------------------------" + flag;
}
#endregion
#region //方法
/// <summary>
/// 合并请求数据
/// </summary>
/// <returns></returns>
private byte[] MergeContent()
{
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;
}
/// <summary>
/// 上传
/// </summary>
/// <param name="requestUrl">请求url</param>
/// <param name="responseText">响应</param>
/// <returns></returns>
public bool Upload(String requestUrl, out String responseText)
{
WebClient 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;
}
/// <summary>
/// 设置表单数据字段
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="fieldValue">字段值</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String fieldValue)
{
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
string httpRowData = String.Format(httpRow, fieldName, fieldValue);
bytesArray.Add(encoding.GetBytes(httpRowData));
}
/// <summary>
/// 设置表单文件数据
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="filename">字段值</param>
/// <param name="contentType">内容内型</param>
/// <param name="fileBytes">文件字节流</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\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
}
public static class HttpUtil
{
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
}
public static Stream GetStream(string url)
{
System.Net.WebRequest webreq = System.Net.WebRequest.Create(url);
System.Net.WebResponse webres = webreq.GetResponse();
return webres.GetResponseStream();
}
public static string Upload(string url,List<UploadFileModel> files) {
using (var content = new MultipartFormDataContent())
{
foreach (var file in files)
{
content.Add(new StreamContent(new MemoryStream(file.data)), file.field, file.filename);
}
var httpClient = new HttpClient();
var responseBody = httpClient.PostAsync(url, content).Result;
if (!responseBody.IsSuccessStatusCode) { }
// Get json string
var responseStr = responseBody.Content.ReadAsStringAsync().Result;
return responseStr;
}
}
/// <summary>
/// 以POST 形式请求数据
/// </summary>
/// <param name="RequestPara"></param>
/// <param name="Url"></param>
/// <returns></returns>
public static string PostData(string RequestPara, string Url)
{
WebRequest hr = HttpWebRequest.Create(Url);
byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);
hr.ContentType = "application/x-www-form-urlencoded";
hr.ContentLength = buf.Length;
hr.Method = "POST";
System.IO.Stream RequestStream = hr.GetRequestStream();
RequestStream.Write(buf, 0, buf.Length);
RequestStream.Close();
System.Net.WebResponse response = hr.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
string ReturnVal = reader.ReadToEnd();
reader.Close();
response.Close();
return ReturnVal;
}
/// <summary>
/// 以GET 形式获取数据
/// </summary>
/// <param name="RequestPara"></param>
/// <param name="Url"></param>
/// <returns></returns>
public static string GetData(string url)
{
var httpClient = new HttpClient();
var responseBody = httpClient.GetAsync(url).Result;
if (!responseBody.IsSuccessStatusCode)
{
throw new Exception("Error:" + responseBody.StatusCode + "." + responseBody.Content.ReadAsStringAsync().Result);
}
return responseBody.Content.ReadAsStringAsync().Result;
}
}