http响应 [英] http response

查看:112
本文介绍了http响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个又一个...下面给出的代码允许从localhost侦听,但我想知道我们是否可以将消息发送回localhost/浏览器..任何如何发送消息的帮助,例如这是测试



使用系统;
使用System.IO;
使用System.Net;
使用System.Net.Sockets;
使用System.Text;

类MyTcpListener
{
公共静态void Main()
{


试试
{

//在端口13000上设置TcpListener.
Int32端口= 80;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

//TcpListener服务器=新的TcpListener(端口);
TcpListener服务器=新的TcpListener(localAddr,port);

//开始监听客户的请求.
server.Start();

//读取数据的缓冲区
Byte []个字节=新的Byte [256];
字符串数据= null;

//进入监听循环.
而(true)
{
Console.Write(正在等待连接...");

//执行阻止调用以接受请求.
//您也可以在此处使用server.AcceptSocket().
TcpClient客户端= server.AcceptTcpClient();
Console.WriteLine("Connected!");

数据= null;

//获取用于读写的流对象
NetworkStream流= client.GetStream();

int i;

//循环以接收客户端发送的所有数据.
while((i = stream.Read(bytes,0,bytes.Length))!= 0)
{
//将数据字节转换为ASCII字符串.
数据= System.Text.Encoding.ASCII.GetString(bytes,0,i);
Console.WriteLine(String.Format("Received:{0}",data));

//处理客户端发送的数据.
数据= data.ToUpper();

byte [] msg = System.Text.Encoding.ASCII.GetBytes(data);

//发送回覆.
stream.Write(msg,0,msg.Length);
Console.WriteLine(正在发送消息..");

}

//关闭和结束连接
client.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("SocketException:{0}",e);
}

Console.WriteLine("\ n点击进入以继续...");
Console.Read();
}
}

hi one and all... the below given code enables to listen from localhost but i was wondering if we could send message back to localhost/browser.. any help how to send message for example like This is test



using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MyTcpListener
{
public static void Main()
{


try
{

// Set the TcpListener on port 13000.
Int32 port = 80;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

// TcpListener server = new TcpListener(port);
TcpListener server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();

// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;

// Enter the listening loop.
while (true)
{
Console.Write("Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected!");

data = null;

// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();

int i;

// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine(String.Format("Received: {0}", data));

// Process the data sent by the client.
data = data.ToUpper();

byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sending message..");

}

// Shutdown and end connection
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}

Console.WriteLine("\nHit enter to continue...");
Console.Read();
}
}

推荐答案

您的基本问题是,什么将监听您的消息,您希望它如何响应?将内容发送到Web服务器的最佳方法是在其上托管Web服务.您可以执行HTTPPost,并且可以尝试使其看起来像是来自网页的回传帖子,但是同样,如果您完全控制服务器,则Web服务是合乎逻辑的选择.
your basic problem is, what is going to listen to your message, and how do you expect it to respond ? Your best way to send something to a web server, is to host a web service on it. You CAN do a HTTPPost, and you could try to make it look like a returning post from a web page, but again, if you have full control over the server, a web service is the logical choice.


这篇关于http响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆