从asmx文件向文本框添加数据 [英] add data to textbox from asmx file
本文介绍了从asmx文件向文本框添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ijust创建一个发送到asmx文件的代码,但我不知道如何将数据发送到我的文本框
ijust create a code for sending to asmx file but I don't know how I get the data to my textbox
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using System.Windows.Forms;
namespace Niles_Money_Transfer
{
class GetClient
{
/*
* The API's response variables
*/
private string response;
public string Response
{
get { return response; }
set { response = value; }
}
/*
* The server address of the GetClient API
*/
private string server;
public string Server
{
get { return server; }
set { server = value; }
}
/*
* Transfer Name
*/
private string myTransfer;
public string MyTransfer
{
get { return myTransfer; }
set { myTransfer = value; }
}
/*
* The data that will be sent to the GetClient API
*/
private string dataToSend;
public string DataToSend
{
get { return dataToSend; }
set { dataToSend = value; }
}
/*
* constructor
*
* Constructs a GetClient object
*
* @param string strUserName Your username
*/
public GetClient(string strUsername)
{
myTransfer = strUsername;
dataToSend = string.Empty;
}
/*
* GetClient::buildPostVariables()
*
* Builds an URL encoded post string which contains the variables to be
* sent to the API in the correct format.
*
* @param string $strCurrency 3 letter ISO-4217 currency code.
*
* @return string The URL encoded post string
*/
public string BuildPostVariables(string strRecord)
{
StringBuilder sbDataToSend = new StringBuilder();
sbDataToSend.AppendFormat("TRANSFER={0}", HttpUtility.UrlEncode(myTransfer), HttpUtility.UrlEncode((string)strRecord));
dataToSend = sbDataToSend.ToString();
return dataToSend;
}
public string PostData()
{
string response = string.Empty;
StreamWriter myWriter = null;
HttpWebRequest objRequest = null;
try
{
// send the post
objRequest = (HttpWebRequest)WebRequest.Create(server);
objRequest.Method = "POST";
objRequest.ContentLength = dataToSend.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(dataToSend);
myWriter.Close();
// read the response
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
StreamReader sr = new StreamReader(objResponse.GetResponseStream());
string result = "";
string strTemp = "";
bool flag = true;
while (flag)
{
strTemp = sr.ReadLine();
if (strTemp != null)
{
result += strTemp;
}
else
{
flag = false;
}
}
response = result;
// decode the response string
response = HttpUtility.UrlDecode(response);
sr.Close();
}
catch (UriFormatException e)
{
throw (e);
}
catch (Exception e)
{
throw (e);
}
finally
{
if (myWriter != null)
{
myWriter.Close();
}
}
return response;
}
}
}
/*
* test GetClient API
*/
GetClient objGetBalanceClient = new GetClient("'" + textBox9.Text + "'");
objGetBalanceClient.Server = "http://localhost:61016/WebService1.asmx";
string strPostString = objGetBalanceClient.BuildPostVariables("");
objGetBalanceClient.Response = objGetBalanceClient.PostData();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Web.Script.Services;
using System.Xml;
namespace WebApplication1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// [System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
private void connectoToMySql(string name)
{
string connString = "SERVER=localhost" + ";" +
"DATABASE=money;" +
"UID=root;" +
"PASSWORD=password;";
MySqlConnection conn = null;
MySqlDataReader rdr = null;
try
{
conn = new MySqlConnection(connString);
conn.Open();
string stm = "SELECT * FROM Add_Number Where E-mail = '" + name + "'";
MySqlCommand cmd = new MySqlCommand(stm, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetInt32(0) + ": "
+ rdr.GetString(1));
}
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
}
推荐答案
strCurrency 3字母ISO-4217货币代码。
*
* @return string网址编码的字符串
* /
public string BuildPostVariables( string strRecord)
{
StringBuilder sbDataToSend = new StringBuilder();
sbDataToSend.AppendFormat( TRANSFER = {0},HttpUtility.UrlEncode(myTransfer) ,HttpUtility.UrlEncode(( string )strRecord));
dataToSend = sbDataToSend.ToString();
return dataToSend;
}
public string PostData()
{
string response = string .Empty;
StreamWriter myWriter = null ;
HttpWebRequest objRequest = null ;
尝试
{
// 发送帖子
objRequest =(HttpWebRequest)WebRequest.Create(server);
objRequest.Method = POST;
objRequest.ContentLength = dataToSend.Length;
objRequest.ContentType = application / x-www-form-urlencoded;
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(dataToSend);
myWriter.Close();
// 阅读回复
HttpWebResponse objResponse =(HttpWebResponse )objRequest.GetResponse();
StreamReader sr = new StreamReader(objResponse.GetResponseStream());
string result = ;
string strTemp = ;
bool flag = true ;
while (flag)
{
strTemp = sr.ReadLine();
if (strTemp!= null )
{
result + = strTemp;
}
其他
{
flag = false ;
}
}
response = result;
// 解码响应字符串
response = HttpUtility.UrlDecode(response) ;
sr.Close();
}
catch (UriFormatException e)
{
throw (e);
}
catch (例外e)
{
throw (e);
}
最后
{
if (myWriter != null )
{
myWriter.Close();
}
}
返回响应;
}
}
}
strCurrency 3 letter ISO-4217 currency code. * * @return string The URL encoded post string */ public string BuildPostVariables(string strRecord) { StringBuilder sbDataToSend = new StringBuilder(); sbDataToSend.AppendFormat("TRANSFER={0}", HttpUtility.UrlEncode(myTransfer), HttpUtility.UrlEncode((string)strRecord)); dataToSend = sbDataToSend.ToString(); return dataToSend; } public string PostData() { string response = string.Empty; StreamWriter myWriter = null; HttpWebRequest objRequest = null; try { // send the post objRequest = (HttpWebRequest)WebRequest.Create(server); objRequest.Method = "POST"; objRequest.ContentLength = dataToSend.Length; objRequest.ContentType = "application/x-www-form-urlencoded"; myWriter = new StreamWriter(objRequest.GetRequestStream()); myWriter.Write(dataToSend); myWriter.Close(); // read the response HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); StreamReader sr = new StreamReader(objResponse.GetResponseStream()); string result = ""; string strTemp = ""; bool flag = true; while (flag) { strTemp = sr.ReadLine(); if (strTemp != null) { result += strTemp; } else { flag = false; } } response = result; // decode the response string response = HttpUtility.UrlDecode(response); sr.Close(); } catch (UriFormatException e) { throw (e); } catch (Exception e) { throw (e); } finally { if (myWriter != null) { myWriter.Close(); } } return response; } } }
/*
* test GetClient API
*/
GetClient objGetBalanceClient = new GetClient("'" + textBox9.Text + "'");
objGetBalanceClient.Server = "http://localhost:61016/WebService1.asmx";
string strPostString = objGetBalanceClient.BuildPostVariables("");
objGetBalanceClient.Response = objGetBalanceClient.PostData();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Web.Script.Services;
using System.Xml;
namespace WebApplication1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// [System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
private void connectoToMySql(string name)
{
string connString = "SERVER=localhost" + ";" +
"DATABASE=money;" +
"UID=root;" +
"PASSWORD=password;";
MySqlConnection conn = null;
MySqlDataReader rdr = null;
try
{
conn = new MySqlConnection(connString);
conn.Open();
string stm = "SELECT * FROM Add_Number Where E-mail = '" + name + "'";
MySqlCommand cmd = new MySqlCommand(stm, conn);
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine(rdr.GetInt32(0) + ": "
+ rdr.GetString(1));
}
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}
finally
{
if (rdr != null)
{
rdr.Close();
}
if (conn != null)
{
conn.Close();
}
}
}
}
}
这篇关于从asmx文件向文本框添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文