- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- ...
private void button1_Click(object sender, EventArgs e)
{
string requestStr = "https://***.ccb.com/***?MERCHANTID***
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestStr);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamReceive, encoding);
richTextBox1.Text = streamReader.ReadToEnd();
}
注意红色加粗的代码,必须有这句才行,否则返回为空
System.Diagnostics.Process.Start(System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe");
Application.Exit();
Winform程序:
发送数据:
string path = AppDomain.CurrentDomain.BaseDirectory + "1.xml";
byte[] SomeBytes = new byte[1024];
WebRequest req = WebRequest.Create(http://localhost:6601/website/1.aspx);
req.Method = "POST";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(path);//要发送xml的路径.
SomeBytes = Encoding.UTF8.GetBytes(xmldoc.OuterXml);
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
MessageBox.Show("发送完毕", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
WebResponse wresp = req.GetResponse(); //接收返回数据
Stream respStream = wresp.GetResponseStream();
StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
richTextBox1.Text = reader.ReadToEnd();
wresp.Close();
Web程序:
接收数据:
Stream s = HttpContext.Current.Request.InputStream;
byte[] buffer = new byte[1024];//无符号 8 位整数数组
int count = 0;
StringBuilder builder = new StringBuilder();//表示可变字符字符串。
while ((count = s.Read(buffer, 0, 1024)) > 0)//每次读取1024个字节
{
builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
}
string msgReturn = builder.ToString();
msgReturn 为接收到的字符串
//发送返回数据
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes("http://www.xt80");
HttpResponse hr = HttpContext.Current.Response;
hr.Clear();
hr.OutputStream.Write(bytes, 0, bytes.Length);
hr.OutputStream.Close();
新建一个名为“app_offline.htm”的文件,放置在网站根目录下,可以使得网站自动定为至该文件显示,比如说要发布新版本的系统,可以实现放这么一个文件作为友情提示,部署完成之后删除即可,注:该文件必须大于512字节,2.0版本有效
1、新建一个WCF Service Library项目,名字叫做WCFServiceLibrary1,代码就用默认的,以下是它的两个方法:
public string MyOperation1(string myValue)
public string MyOperation2(DataContract1 dataContractValue)
2、在WCFServiceLibrary1项目下新建一个类文件,名字叫做MyCustomValidator.cs,编写如下代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
namespace WCFServiceLibrary1{
public class MyCustomValidator:UserNamePasswordValidator{
public override void Validate(string userName, string password)
{
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
if (userName != "123456" || password != "123456")
throw new Exception("用户名和密码错误");
else
throw new Exception("用户名和密码不能为空");
}
} //为了节约地方,就这么堆起了
3、新建一个WCFService项目,名字是WCFService1,引用前面已经编译好的WCFServiceLibrary1.dll,打开Service.svc
写入以下代码:
<% @ServiceHost Language=C# Debug="true" Service="WCFServiceLibrary1.service1" CodeBehind="" %>
使用Makecert 工具创建一个证书,命令如下:
Makecert -r -pe -n "CN=MyServer" -ss My -sky exchange
4、Web.Config做如下配置,并运行WCFService项目:
<system.serviceModel>
<services>
<service behaviorConfiguration="MyBehavior" name="WCFServiceLibrary1.service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="myBinding"
contract="WCFServiceLibrary1.IService1" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceCredentials>
<serviceCertificate findValue="MyServer" storeName="My" x509FindType="FindBySubjectName" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WCFServiceLibrary1.MyCustomValidator,WCFServiceLibrary1" />
</serviceCredentials>
<serviceDebug httpsHelpPageEnabled="false" />
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="myBinding">
<security>
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
6、使用svcutil.exe 工具生成客户端配置文件和代理类,使用方法:
svcutil.exe http://localhost:4207/WCFService1/Service.svc?wsdl
7、新建一个Windows程序,叫做WindowsApplication1,首先添加如下引用:
System.ServiceModel
System.Runtime.Serialization
8、将前面生成的配置文件中的节点Copy到App.Config中,将代理类复制到WindowsApplication1下,在窗体上增加一个按钮,并写入下面的代码:
private void button1_Click(object sender, EventArgs e)
{
Service1Client client = new Service1Client();
client.ClientCredentials.UserName.UserName = "123456";
client.ClientCredentials.UserName.Password = "123456";
MessageBox.Show(client.MyOperation1("www.xt80.com"));
}
运行WindowsApplication1,将会看到返回信息:Hello:www.xt80.com
string poststr = "<?xml version='1.0' encoding='UTF-8'?>";
poststr += "<MasMessage xmlns='http://www.xxx.com/interface'>";
poststr += " <version>1.0</version>";
poststr += " <TxnMsgContent>";
poststr += " <txnType>测试测试测试</txnType>";
poststr += " <cardNo>888888</cardNo>";
poststr += " </TxnMsgContent>";
poststr += "</MasMessage>";
DataSet ds = new DataSet();
StringReader getsr = new StringReader(poststr );
ds.ReadXml(getsr);
以下代码是在WinForm中完成的:
第一步:先将需要Post的XML写成一个文件,这里写成test.xml,调用的时候将其中的参数修改以下就可以了,以下是一个样本:
<?xml version="1.0" encoding="utf-8"?>
<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/">
<soap:Body>
<GetOrderformInfo xmlns="http://samples.com/">
<orderid>00001</orderid>
<leaguerid>0</leaguerid>
<prjtype>4</prjtype>
</GetOrderformInfo>
</soap:Body>
</soap:Envelope>
第二步:CS代码:
/// <summary>
/// 调用WebService的方法
/// </summary>
/// <param name="filename">POST所用文件</param>
/// <param name="requestURL">调用地址</param>
/// <param name="SOAPActionURL"></param>
/// <returns></returns>
private string Post(string filename,string requestURL,string SOAPActionURL)
{
XmlDataDocument doc = new XmlDataDocument();
doc.Load(filename);
byte[] data = System.Text.Encoding.UTF8.GetBytes(doc.OuterXml);
WebRequest request = WebRequest.Create(requestURL);
request.Method = "POST";
request.Headers.Add("SOAPAction", SOAPActionURL);
request.Headers.Add("ContentLength", data.Length.ToString());
request.ContentType = "text/xml; charset=utf-8";
Stream OWrite = request.GetRequestStream();
OWrite.Write(data, 0, data.Length);
OWrite.Close();
Stream oRead = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
oRead = response.GetResponseStream();
XmlDataDocument resultdoc = new XmlDataDocument();
resultdoc.Load(oRead);
return resultdoc.InnerText.ToString();
}
private void button5_Click(object sender, EventArgs e)
{
string filename = AppDomain.CurrentDomain.BaseDirectory + "xmlfiles/UpdatePaymentInfo.xml";
string requestURL = "http://localhost/samples/test.asmx?op=MyTest";
string SOAPActionURL = "http://samples.com/MyTest";
/*这里可以增加修改XML文件参数的代码....*/
richTextBox1.Text = Post(filename, requestURL, SOAPActionURL).Trim(); //这里调用后显示结果
}
注:XML样本和SOAPAction地址在WebService的请求和响应示例里面有说明
1:在“服务(本地)”里有一项Eventlog和HTTP SSL都和IIS的或World Wide Web Publishing Services所以来的服务项 打开它们
2:在“服务(本地)”里中的World Wide Web Publishing Services所需要依赖的服务(Dependencies),看到是IIS Admin Service。进而发现IIS Admin Service也无法启动,再查IIS Admin Service的Dependencies,查的结果是Protected Storage和RPC(Remote Procedure Call),于是启动RPC,接着顺利启动IIS Admin Service,接着启动World Wide Web Publishing Services,于是IIS恢复正常。
注:若启动World Wide Web Publishing Services服务出现127错误(找不到指定的程序),原因是在微软补丁KB939373,卸载掉就可以了
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- ...