本文整理汇总了C#中ASCIIEncoding.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# ASCIIEncoding.GetBytes方法的具体用法?C# ASCIIEncoding.GetBytes怎么用?C# ASCIIEncoding.GetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ASCIIEncoding
的用法示例。
在下文中一共展示了ASCIIEncoding.GetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: sendMessage
public string sendMessage()
{
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] userPassBytes = encoding.GetBytes(String.Format("{0}:{1}", appId, password));
String authHeader = "Authorization: Basic " + (Convert.ToBase64String(userPassBytes));
string postData = "version=1.0" + "&address=" + address + "&message=" + message;
byte[] byteArray = encoding.GetBytes(postData);
WebRequest request = WebRequest.Create("http://192.168.0.250:65182/");
request.Headers.Add(authHeader);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
示例2: TryConnect
//пытаемся отправить кому-то сообщение о том, что хотим понаблюдать за его игрой
//если прокатывает - в ответ нам начнут приходить сообщения о состоянии игры
public void TryConnect(string ip, string port)
{
try
{
TcpClient tcpclnt = new TcpClient();
tcpclnt.Connect(IPAddress.Parse(ip), int.Parse(port));
String str = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[255];
int k = stm.Read(bb, 0, 255);
string an = "";
for (int i = 0; i < k; i++)
an += Convert.ToChar(bb[i]);
stm.Close();
tcpclnt.Close();
}
catch (Exception e)
{
Debug.LogError(e.StackTrace);
}
}
示例3: Main
public static void Main()
{
try
{
TcpListener Listener;
Socket client_socket;
acceptconnection(out Listener, out client_socket);
Receive(client_socket);
//int gh = testReceive(client_socket);
ASCIIEncoding asen = new ASCIIEncoding();
client_socket.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
client_socket.Close();
Listener.Stop();
}
catch (Exception error)
{
Console.WriteLine("Error..... " + error.StackTrace);
}
}
示例4: stringToBase64
//============================================================================
public static String stringToBase64(String str)
{
var encoding = new ASCIIEncoding();
byte[] buf = encoding.GetBytes(str);
int size = buf.Length;
char[] ar = new char[((size + 2) / 3) * 4];
int a = 0;
int i = 0;
while (i < size)
{
byte b0 = buf[i++];
byte b1 =(byte) ((i < size) ? buf[i++] : 0);
byte b2 = (byte) ((i < size) ? buf[i++] : 0);
ar[a++] = BASE64_ALPHABET[(b0 >> 2) & 0x3f];
ar[a++] = BASE64_ALPHABET[((b0 << 4) | ((b1 & 0xFF) >> 4)) & 0x3f];
ar[a++] = BASE64_ALPHABET[((b1 << 2) | ((b2 & 0xFF) >> 6)) & 0x3f];
ar[a++] = BASE64_ALPHABET[b2 & 0x3f];
}
int o=0;
switch (size % 3)
{
case 1: ar[--a] = '='; break;
case 2: ar[--a] = '='; break;
}
return new String(ar);
}
示例5: Main
static int Main ()
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create ("http://localhost:8081/Default.aspx");
request.Method = "POST";
ASCIIEncoding ascii = new ASCIIEncoding ();
byte [] byData = ascii.GetBytes ("Mono ASP.NET");
request.ContentLength = byData.Length;
Stream rs = request.GetRequestStream ();
rs.Write (byData, 0, byData.Length);
rs.Flush ();
try {
HttpWebResponse response = (HttpWebResponse) request.GetResponse ();
using (StreamReader sr = new StreamReader (response.GetResponseStream (), Encoding.UTF8, true)) {
string result = sr.ReadToEnd ();
if (result.IndexOf ("<p>REQ:0</p>") == -1) {
Console.WriteLine (result);
return 1;
}
}
response.Close ();
} catch (WebException ex) {
if (ex.Response != null) {
StreamReader sr = new StreamReader (ex.Response.GetResponseStream ());
Console.WriteLine (sr.ReadToEnd ());
} else {
Console.WriteLine (ex.ToString ());
}
return 2;
}
return 0;
}
示例6: PostHtmlFromUrl
/// <summary>
/// �����������վ��UTF-8���룬Http Post�������Ҳ��Ҫ��UTF-8����
/// HttpUtility.UrlEncode(merId, myEncoding)
/// </summary>
/// <param name="url">���ʵ�ַ����������</param>
/// <param name="para">�����ַ���</param>
/// <returns></returns>
public static string PostHtmlFromUrl(string url, string postData)
{
String sResult = "";
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
request.ContentLength = postData.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
return content;
}
catch (Exception e)
{
sResult = "-101";
return sResult;
}
}
示例7: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
string url = "https://graph.facebook.com/567517451/notifications";
HttpWebRequest httpWReq =
(HttpWebRequest)WebRequest.Create(url);
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "access_token=355242331161855|qyYMEnPyR2y3sWK8H7rN-6n3lBU";
postData += "&template=Test";
postData += "&href=http://postaround.me";
byte[] data = encoding.GetBytes(postData);
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
示例8: sendMessage
public static void sendMessage(Socket clientSocket)
{
ASCIIEncoding asn = new ASCIIEncoding();
String sendString = Console.ReadLine();
//String sendString = "This is a test!!!!!!!!!!!!!!!!!!!!!!!!";
clientSocket.Send(asn.GetBytes(sendString));
}
示例9: CutString
/// <summary>
/// 截取字符长度
/// </summary>
/// <param name="inputString">字符</param>
/// <param name="len">长度</param>
/// <returns></returns>
public static string CutString(string inputString, int len)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}
try
{
tempString += inputString.Substring(i, 1);
}
catch
{
break;
}
if (tempLen > len)
break;
}
//如果截过则加上半个省略号
byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
if (mybyte.Length > len)
tempString += "…";
return tempString;
}
示例10: echoMessage
public static int echoMessage(Socket clientSocket)
{
string messageToSend = testReceive(clientSocket);
ASCIIEncoding asn = new ASCIIEncoding();
clientSocket.Send(asn.GetBytes(messageToSend));
return messageToSend.Length;
}
示例11: DoPosTest
private void DoPosTest(ASCIIEncoding ascii, string source, int charIndex, int count, byte[] bytes, int byteIndex)
{
int actualValue;
actualValue = ascii.GetBytes(source, charIndex, count, bytes, byteIndex);
Assert.True(VerifyASCIIEncodingGetBytesResult(ascii, source, charIndex, count, bytes, byteIndex, actualValue));
}
示例12: Main
public static void Main() {
try {
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("172.21.5.99",8001); // use the ipaddress as in the server program
Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");
String str=Console.ReadLine();
Stream stm = tcpclnt.GetStream();
ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine("Transmitting.....");
stm.Write(ba,0,ba.Length);
byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(bb[i]));
tcpclnt.Close();
}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}
示例13: Main
public static void Main()
{
try {
TcpClient tcpclnt = new TcpClient();
//Console.WriteLine("Connecting.....");
tcpclnt.Connect("161.115.86.57",8001);
// use the ipaddress as in the server program
//Console.WriteLine("Connected");
//Console.Write("Enter the string to be transmitted : ");
String sendString = Console.ReadLine();
Stream serverSendStream = tcpclnt.GetStream();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] bytesInSend = asen.GetBytes(sendString);
//Console.WriteLine("Transmitting.....");
// send length then string to read to length+1
serverSendStream.Write(bytesInSend, 0, bytesInSend.Length);
byte[] bytesToRead = new byte[100];
int numberOfBytesRead = serverSendStream.Read(bytesToRead, 0, bytesToRead.Length);
for (int i = 0; i < numberOfBytesRead; i++)
Console.Write(Convert.ToChar(bytesToRead[i]));
tcpclnt.Close();
}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}
示例14: StartInjection
public static bool StartInjection(string DllName, uint ProcessID)
{
bool flag;
try
{
IntPtr hProcess = new IntPtr(0);
IntPtr lpBaseAddress = new IntPtr(0);
IntPtr lpStartAddress = new IntPtr(0);
IntPtr hHandle = new IntPtr(0);
int nSize = DllName.Length + 1;
hProcess = OpenProcess(0x1f0fff, false, ProcessID);
if (!(hProcess != IntPtr.Zero))
{
throw new Exception("Processus non ouvert...injection \x00e9chou\x00e9e");
}
lpBaseAddress = VirtualAllocEx(hProcess, IntPtr.Zero, (UIntPtr) nSize, 0x1000, 0x40);
if (!(lpBaseAddress != IntPtr.Zero))
{
throw new Exception("M\x00e9moire non allou\x00e9e...injection \x00e9chou\x00e9e");
}
ASCIIEncoding encoding = new ASCIIEncoding();
int lpNumberOfBytesWritten = 0;
if (!WriteProcessMemory(hProcess, lpBaseAddress, encoding.GetBytes(DllName), nSize, lpNumberOfBytesWritten))
{
throw new Exception("Erreur d'\x00e9criture dans le processus...injection \x00e9chou\x00e9e");
}
lpStartAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
if (!(lpStartAddress != IntPtr.Zero))
{
throw new Exception("Adresse LoadLibraryA non trouv\x00e9e...injection \x00e9chou\x00e9e");
}
hHandle = CreateRemoteThread(hProcess, IntPtr.Zero, 0, lpStartAddress, lpBaseAddress, 0, 0);
if (!(hHandle != IntPtr.Zero))
{
throw new Exception("Probl\x00e8me au lancement du thread...injection \x00e9chou\x00e9e");
}
uint num3 = WaitForSingleObject(hHandle, 0x2710);
if (((num3 == uint.MaxValue) && (num3 == 0x80)) && ((num3 == 0) && (num3 == 0x102)))
{
throw new Exception("WaitForSingle \x00e9chou\x00e9 : " + num3.ToString() + "...injection \x00e9chou\x00e9e");
}
if (!VirtualFreeEx(hProcess, lpBaseAddress, 0, 0x8000))
{
throw new Exception("Probl\x00e8me lib\x00e8ration de m\x00e9moire...injection \x00e9chou\x00e9e");
}
if (hHandle == IntPtr.Zero)
{
throw new Exception("Mauvais Handle du thread...injection \x00e9chou\x00e9e");
}
CloseHandle(hHandle);
return true;
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
flag = false;
}
return flag;
}
示例15: WriteMessageDelegate
/*private void WriteMessage(string msg)
{
if (this.rtbServer.InvokeRequired)
{
WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
this.rtbServer.Invoke(d, new object[] { msg });
}
else
{
this.rtbServer.AppendText(msg + Environment.NewLine);
}
}*/
/// <summary>
/// Echo the message back to the sending client
/// </summary>
/// <param name="msg">
/// String: The Message to send back
/// </param>
/// <param name="encoder">
/// Our ASCIIEncoder
/// </param>
/// <param name="clientStream">
/// The Client to communicate to
/// </param>
private void Echo(string msg, ASCIIEncoding encoder, NetworkStream clientStream)
{
// Now Echo the message back
byte[] buffer = encoder.GetBytes(msg);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}