http://tw.myblog.yahoo.com/wululu-blog/article?mid=170&next=169&l=f&fid=16
把上面這個例子再簡化說明清楚一點
VS新增一個控制台專案 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string tRequestFilePath = @"D:\\a.ini";
string tUrl = "http://smexpress.mitake.com.tw:9600/SmSendPost.asp?username=XXXXXXXA&password=11111111&encoding=Big5";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tUrl);
request.Timeout = 1000 * 5;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//撈出發送資料放到tMSG存成格式為ANSI(必須存成INI格式喔)這裡我直接用一個ini檔來簡化這個例子
// StreamWriter sw = new StreamWriter(tRequestFilePath, true, System.Text.Encoding.Default);
// sw.Write(tMSG.ToString());
// sw.Close();
//再讀出來
StreamReader sr = new StreamReader(tRequestFilePath, System.Text.Encoding.Default);
string strTest = sr.ReadToEnd();
sr.Close();
//轉成byte格式
Byte[] B = System.Text.Encoding.Default.GetBytes(strTest);
request.ContentLength = B.Length;
System.IO.Stream ioStream = request.GetRequestStream(); //開始提交數據
ioStream.Write(B, 0, B.Length);
ioStream.Close();
//取得回來訊息
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string tReturn = reader.ReadToEnd();
//可以輸出 或存檔tReturn
Console.WriteLine(tReturn);
Console.ReadKey();
reader.Close(); }
}
}
D:\\a.ini (ansi格式)內容如下
[101]
DestName=經理
dstaddr=0980944485
smbody=我是測試1
response=http://192.168.1.200/smreply.asp
[102]
DestName=二寶
dstaddr=0982944445
smbody=我是測試2
[103]
DestName=小明
dstaddr=0981444444
smbody=我是測試3
沒有留言:
張貼留言