php代码转.net,http post请求,谁能够帮我把这个php代码转为.net的代码

主要是数组上传。比如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 //方法

///

/// 合并请求数据

///

///

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;

}

///

/// 上传

///

/// 请求url

/// 响应

///

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;

}

///

/// 设置表单数据字段

///

/// 字段名

/// 字段值

///

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));

}

///

/// 设置表单文件数据

///

/// 字段名

/// 字段值

/// 内容内型

/// 文件字节流

///

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 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;

}

}

///

/// 以POST 形式请求数据

///

///

///

///

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;

}

///

/// 以GET 形式获取数据

///

///

///

///

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值