2017年10月20日 星期五
C# x WebService x 移除
看了 當麻許的Web Service移除 這篇
然後覺得他遇到的狀況跟我差不多
然後自己又琢磨了一下
然後我的開發工具是VS2015、WebSite、Framework4.5
紅字是重點
///////////以下寫在.asmx裡面///////////
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web.Services;
using System.Web.Script.Services;
namespace WebSite1
{
[WebService(Namespace = "",Description ="")]
[System.ComponentModel.ToolboxItem(false)]
public class iWS : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void PostGetData()
{
try
{
Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
string strjson = Encoding.UTF8.GetString(b);
JObject jObjt = JObject.Parse(strjson);
Context.Response.Output.Write(JsonConvert.SerializeObject(jObjt, Formatting.Indented));
}
catch (Exception ex)
{
Context.Response.Output.Write(JsonConvert.SerializeObject(ex.Message, Formatting.Indented));
}
}
}
}
///////////以上寫在.asmx裡面///////////
///////////以下Web.config///////////
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
<add name="Documentation"/>
</protocols>
<conformanceWarnings>
<remove name='BasicProfile1_1'/>
</conformanceWarnings>
</webServices>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
</configuration>
///////////以上Web.config///////////
參考資料 :
https://dotblogs.com.tw/junegoat/2012/09/10/c-sharp-webservice-remove-xmlns
https://stackoverflow.com/questions/12705380/remove-xml-tag-while-creating-web-service-in-asp-net
https://taskyam.israports.co.il/TaskYamWS/GeneralWebServices.asmx
https://social.msdn.microsoft.com/Forums/zh-TW/be1c812b-503e-4d35-9f0c-9f12b9aaf406/how-to-remove-xmlns-attribute-from-webservice-response?forum=asmxandxml
2017年10月8日 星期日
C# x WebService x JSON x 接收及傳值
WebService 接收這個部分找了超多資料,搞了三天才搞定
-------以下WebService的程式碼,寫在asmx裡面
using System.Web.Services;
using System.Web.Script.Services;
using System.IO;
using System.Text;
using Newtonsoft.Json.Linq;
namespace WebService
{
[WebService(Namespace = "http://microsoft.com/webservices/")]
public class iWS : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string PostGetData()
{
//應用程式呼叫的網址 : "http://localhost:54756/iWS.asmx/PostGetData"
Stream s = System.Web.HttpContext.Current.Request.InputStream;
byte[] b = new byte[s.Length];
s.Read(b, 0, (int)s.Length);
string aa = Encoding.UTF8.GetString(b);
return aa;
}
}
}
-------以上WebService的程式碼,寫在asmx裡面
-------以下ConsoleApplication,寫在Program.cs裡面
using System;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string targetUrl = "http://localhost/iWS.asmx/PostGetData";
string connString = "你資料庫的位置";
byte[] postData;
string jsonStr;
try
{
string strSQL = "SELECT 'H123' as a, " + "\r\n" +
"'56789' as b, " + "\r\n" +
"'2' as c, ";
SqlConnection connSQL = new SqlConnection(connString);
connSQL.Open();
SqlCommand comm = new SqlCommand(strSQL, connSQL);
SqlDataReader dr = comm.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dr.Close();
comm.Dispose();
connSQL.Close();
int dtCount = dt.Rows.Count;
for (int i = 0; i < dtCount; i++)
{
JObject obj = new JObject(
new JProperty("a", dt.Rows[i][0].ToString()),
new JProperty("b", dt.Rows[i][1].ToString()),
new JProperty("c", dt.Rows[i][2].ToString())
);
jsonStr = JsonConvert.SerializeObject(obj, Formatting.Indented);
postData = Encoding.UTF8.GetBytes(jsonStr);
HttpWebRequest request = HttpWebRequest.Create(targetUrl) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Timeout = 30000;
request.ContentLength = postData.Length;
// 寫入 Post Body Message 資料流
using (Stream st = request.GetRequestStream())
{
st.Write(postData, 0, postData.Length);
Console.WriteLine(jsonStr);
}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string result = sr.ReadToEnd();
Console.WriteLine(result);
}
}
}
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
-------以上ConsoleApplication,寫在Program.cs裡面
參考資料 :
http://www.itread01.com/articles/1478475314.html
https://kknews.cc/zh-tw/other/4egm2v.html
訂閱:
文章 (Atom)