1. [代码][C#]代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Reflection;
namespace ConsoleApplication1
{
internal class user
{
public string content { get; set; }
public string mdpass { get; set; }
public string mobile { get; set; }
public string name { get; set; }
public string pass { get; set; }
public string ptimestamp { get; set; }
public string sendPort { get; set; }
public string sendTime { get; set; }
public string title { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("********WebService Testing!*******");
HttpHelper http = new HttpHelper();
Dictionary<string, string> dics = new Dictionary<string, string>();
user bean = new user();
bean.name = "122";
bean.pass = "21";
bean.ptimestamp = "20121109111950";
bean.content = "测试";
bean.mobile = "13432033335";
bean.sendPort = "";
bean.sendTime = "";
bean.mdpass = "4CC4122226B23F75EDE78";
http.NameSpace = "http://*.*.*.*:8080//sms";
http.WEncoding = Encoding.UTF8;
Console.WriteLine(http.GetWebServiceStr("http://124.****:8080//sms",
"SendSMS", http.CreateSoap(bean)));
Console.WriteLine("*********Get Testing!***********");
http.WEncoding = Encoding.UTF8;
http.IniStalling();
Console.WriteLine(http.MethodGetHttpStr("http://*.*.*.*:8080/smshttp?"));
Console.WriteLine("*********Post Testing!**********");
http.WEncoding = Encoding.UTF8;
http.IniStalling();
Console.WriteLine(http.MethodPostHttpStr("http://*.*.*.*:8080/smshttp?act=getbalance", ""));
Console.ReadLine();
}
}
/// <summary>
/// socket核心
/// </summary>
internal class HttpHelper
{
public HttpHelper()
{
IniStalling();
}
public void IniStalling()
{
HttpHeaders = new List<HttpHeaderModel>();
ResponseHttpHeaders = new List<HttpHeaderModel>();
DicCookies = new List<CookiesModel>();
AddHttpHeader("Accept", "*/*");
AddHttpHeader("Accept-Language", "zh-CN");
AddHttpHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
AddHttpHeader("UA-CPU", "AMD64");
AddHttpHeader("Connection", "Keep-Alive");
}
public Encoding WEncoding = Encoding.UTF8;
/// <summary>
/// 请求或者返回的头文件
/// </summary>
internal class HttpHeaderModel
{
public string Key { get; set; }
public string Value { get; set; }
}
/// <summary>
/// get,post的cookies
/// </summary>
internal class CookiesModel
{
public string Key { get; set; }
public string Value { get; set; }
public string Domain { get; set; }
}
internal List<HttpHeaderModel> HttpHeaders = new List<HttpHeaderModel>();
internal List<HttpHeaderModel> ResponseHttpHeaders = new List<HttpHeaderModel>();
internal List<CookiesModel> DicCookies = new List<CookiesModel>();
/// <summary>
/// 添加HTTP头
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddHttpHeader(string key, string value)
{
foreach (HttpHeaderModel httpHeaderModel in HttpHeaders)
{
if (httpHeaderModel.Key == key)
{
httpHeaderModel.Value = value;
return;
}
}
HttpHeaders.Add(new HttpHeaderModel()
{
Key = key,
Value = value
});
}
public string MethodGetHttpStr(string url)
{
return GetHttpByte(url, null);
}
public string MethodPostHttpStr(string url, string data)
{
return GetHttpByte(url, data);
}
/// <summary>
/// 设置命名空间,请在地址后面加上wsdl获取。
/// </summary>
public string NameSpace { get; set; }
private int index = 0;
public string CreateSoap(object obj)
{
StringBuilder sb = new StringBuilder();
Type tType = obj.GetType();
PropertyInfo[] pInfos = tType.GetProperties();
sb.AppendLine("<test" + index + " xsi:type="m" + index + ":" + tType.Name + "">");
foreach (PropertyInfo pInfo in pInfos)
{
sb.AppendLine(string.Format(" <{0}>{1}</{0}>", pInfo.Name, pInfo.GetValue(obj, null)));
}
sb.AppendLine("</test" + index + ">");
index++;
return sb.ToString();
}
public string CreateSoap(Dictionary<string, string> MethodParms)
{
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> keyValuePair in MethodParms)
{
sb.AppendLine(string.Format(" <{0}>{1}</{0}>", keyValuePair.Key, keyValuePair.Value));
}
return sb.ToString();
}
public string GetWebServiceStr(string url, string MethodName, string soap)
{
index = 0;
if (string.IsNullOrEmpty(NameSpace))
throw new MissingFieldException("请输入NameSpace");
if (url.Contains("asmx"))
AddHttpHeader("SOAPAction", """ + NameSpace.TrimEnd('/') + "/" + MethodName + """);
else
AddHttpHeader("SOAPAction", """");
AddHttpHeader("Content-Type", "text/xml; charset=utf-8");
StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version="1.0" encoding="utf-8"?>");
sb.AppendLine("<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">");
sb.AppendLine("<soap:Body>");
sb.AppendLine(string.Format("<" + MethodName + " xmlns="" + NameSpace + "">"));
sb.Append(soap);
sb.AppendLine(string.Format("</" + MethodName + ">"));
sb.AppendLine("</soap:Body>");
sb.AppendLine("</soap:Envelope>");
return MethodPostHttpStr(url, sb.ToString());
}
public string GetHttpByte(string url, string data = "")
{
bool methodPost = !string.IsNullOrEmpty(data);
if (methodPost)
{
byte[] sendBytes = WEncoding.GetBytes(data);
AddHttpHeader("Content-Length", sendBytes.Length.ToString());
}
string cookies =
DicCookies.Aggregate(string.Empty,
(current, cookie) => current + string.Format("{0}:{1};", cookie.Key, cookie.Value));
string[] urlspils = url.Replace("http://", "").Split('/');
string host = urlspils[0];
string methodurl = url.Replace("http://", "").Remove(0, host.Length);
string[] ipport = host.Split(':');
string ip = "127.0.0.1";
string post = "80";
if (ipport.Length > 1)
{
host = ipport[0];
post = ipport[1];
}
IPAddress[] addressList = Dns.GetHostAddresses(host);
if (addressList.Length > 0)
{
ip = addressList[0].ToString();
}
Socket httpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint serverHost = new IPEndPoint(IPAddress.Parse(ip), int.Parse(post));
StringBuilder httpHeader = new StringBuilder();
httpHeader.Append((methodPost ? "POST" : "GET") + " " + methodurl + " HTTP/1.1
");
AddHttpHeader("Host", host);
if (!string.IsNullOrEmpty(cookies))
AddHttpHeader("Cookie", cookies);
foreach (var item in HttpHeaders)
{http://www.huiyi8.com/hunsha/lifu/
httpHeader.Append(string.Format("{0}: {1}
", item.Key, item.Value));
}婚纱礼服欣赏
string httpData = string.Format("{0}
{1}", httpHeader, data);
Console.WriteLine(httpData);
try
{
httpSocket.Connect(serverHost);
if (!httpSocket.Connected)
throw new WebException("连接不上服务器");