using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace ImageClient.Common
{
public class HttpHelper
{
public bool UploadImage(string fullName) {
return true;
}
#region 获取指定WEB页面
/// GET方法获取页面
/// 函数名:GetWebUrl
/// 功能描述:WebClient 获取页面
/// 处理流程:
/// 算法描述:
/// 作 者: lxc
/// 日 期: 2012-10-09
/// 修 改:
/// 日 期:
/// 版 本:
public static string GetWebUrl(string strurl)
{
WebClient webClient = new WebClient();
try
{
webClient.Credentials = CredentialCache.DefaultCredentials;
Byte[] pageData = webClient.DownloadData(strurl);
//string pageHtml = Encoding.UTF8.GetString(pageData);
string pageHtml = Encoding.Default.GetString(pageData);
return pageHtml;
}
catch (WebException webEx)
{
return "error_GetWebUrl:" + webEx.ToString();
}
finally
{
webClient = null;
}
}
#endregion
#region PostUrl(String url, String paramList)
///
/// POST方法获取页面
///
///
/// 格式: a=xxx&b=xxx&c=xxx
///
public static string PostUrl(String url, String paramList)
{
HttpWebResponse res = null;
string strResult = "";
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Timeout = 3000;
req.Method = "POST";
req.KeepAlive = true;
//req.ContentType = "application/josn";
req.ContentType = "application/x-www-form-urlencoded";
//req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (paramList != null)
{
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(reserved, i);
if (j == -1)
{
// UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
UrlEncoded.Append((paramList.Substring(i, paramList.Length - i)));
break;
}
// UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
UrlEncoded.Append((paramList.Substring(i, j - i)));
UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
// Encoding encode = System.Text.Encoding.Default;//GetEncoding("utf-8");
string encodeheader = res.ContentType;
string encodestr = System.Text.Encoding.UTF8.HeaderName;
if ((encodeheader.IndexOf("charset=") >= 0) && (encodeheader.IndexOf("charset=GBK") == -1) && (encodeheader.IndexOf("charset=gbk") == -1))
{
int i = encodeheader.IndexOf("charset=");
encodestr = encodeheader.Substring(i + 8);
}
Encoding encode = System.Text.Encoding.GetEncoding(encodestr);//GetEncoding("utf-8");
StreamReader sr = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
}
catch (Exception e)
{
strResult = e.ToString();
}
finally
{
if (res != null)
{
res.Close();
}
}
return strResult;
}
#endregion
///
/// WebClient上传文件至服务器
///
/// 文件名,全路径格式
/// 服务器文件夹路径
///
public bool UpLoadFile(string localFilePath, string url)
{
string uriString;
// 得到文件名,文件扩展名字,服务器路径
uriString = url + localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
myWebClient.Headers.Add("Content-Type", "multipart/form-data;charset=utf-8;boundary=.png");
// 要上传的文件
FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(url, "POST");
if (postStream.CanWrite)
{
postStream.Write(postArray, 0, postArray.Length);
}
else
{
// MessageBox.Show("文件目前不可写!");
}
postStream.Close();
}
catch
{
//MessageBox.Show("文件上传失败,请稍候重试~");
return false;
}
return true;
}
public bool UpLoadFileExe(string url, string fileanme) {
string exename = "html.exe";
// @"-url http://127.0.0.1:9900/upload/k001/zf1x -filename C:/GOPATH/src/eassoft/imageserver/html/广东美的集团有限公司-公章1.bmp";
string formatcmd = "-url \"{0}\" -filename \"{1}\"";
string cmd = string.Format(formatcmd,url,fileanme);
Process myprocess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo(exename, cmd);
myprocess.StartInfo = startInfo;
//通过以下参数可以控制exe的启动方式,具体参照 myprocess.StartInfo.下面的参数,如以无界面方式启动exe等
myprocess.StartInfo.UseShellExecute = false;
myprocess.StartInfo.CreateNoWindow = true;
myprocess.Start();
myprocess.WaitForExit(30000);
myprocess.Close();
return true;
}
}
}