问题描述
我正在为UWP应用程序(C#)开发一个Http代理服务器,但是面对创建套接字发送和接收的请求的问题。我首先创建一个套接字流对象,然后监听send-async发送给套接字的Http请求流侦听器。但是,在完全发送请求之前,我面临Http异常和套接字关闭,我怀疑这两个数据没有正确地发送到套接字流监听器/套接字流监听器没有正确接收。我在哪里出错?
我的尝试:
Im developing an Http proxy server for a UWP application(C#) but face issues with requests sent and received by a creating a socket.I first create a socket stream object and then listen for Http requests sent by send-async to socket stream listener.However i face Http exceptions and sockets closing before requests are sent completely, the cause of this i suspect that either data is not sent to socket stream listener properly/not received by socket stream listener properly.Where am i going wrong here?
What I have tried:
//Called for each request received on StreamSocketListner , a new task is created to make connections to remote Proxy and retirve data from backend.
public async Task listenerTask(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
try
{
Debug.WriteLine("In listenerTask************ ");
string snirequest = null;
string hostName = null;
string reqUri = null;
string uriReqStr = null;
String orgRequest = null;
using (IInputStream input = args.Socket.InputStream)
{
//Reading data arrived at socket
orgRequest = await GetSocketDataString(input);
}
reqUri = getOriginalRequestUri(orgRequest);
Debug.WriteLine("*************Org uri req", reqUri);
string totalHostName = null;
if (reqUri != null)
{
if (reqUri.StartsWith("https"))
{
Debug.WriteLine("Req is https");
if (orgRequest.Contains("redirectrequest") == false)
totalHostName = reqUri.Substring(8, reqUri.Length - 8).TrimEnd('/');
else
totalHostName = reqUri.Substring(8, reqUri.Length - 8);
int uriIndx = totalHostName.IndexOf('/');
if (uriIndx > 0)
{
uriReqStr = totalHostName.Substring(uriIndx);
hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1).TrimEnd('/'); ;
}
else
{
uriReqStr = "/";
hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1);
}
Debug.WriteLine(hostName);
Debug.WriteLine(uriReqStr);
snirequest = "CONNECT " + hostName + ":443 HTTP/1.1\r\nHost: " + hostName + ":443\r\nProxy-Connection: Keep-Alive\r\nConnection: Keep-Alive\r\n\r\n";
}
else
{
if (orgRequest.Contains("redirectrequest") == false)
totalHostName = reqUri.Substring(7, reqUri.Length - 7).TrimEnd('/');
else
totalHostName = reqUri.Substring(7, reqUri.Length - 7);
int uriIndx = totalHostName.IndexOf('/');
if (uriIndx > 0)
{
uriReqStr = totalHostName.Substring(uriIndx);
hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1).TrimEnd('/'); ;
}
else
{
uriReqStr = "/";
hostName = totalHostName.Substring(0, totalHostName.Length - uriReqStr.Length + 1);
}
Debug.WriteLine(hostName);
Debug.WriteLine(uriReqStr);
snirequest = getSniRequest(reqUri, orgRequest, hostName, uriReqStr);
}
//Debug.WriteLine(snirequest);
Byte[] bytesSent = System.Text.Encoding.ASCII.GetBytes(snirequest);
// Create a socket connection with the specified server and port.
Windows.Networking.Sockets.StreamSocket socket = new Windows.Networking.Sockets.StreamSocket();
socket.Control.KeepAlive = true;
Windows.Networking.HostName serverHost = new Windows.Networking.HostName(PROXY_IP);
string serverPort = PROXY_PORT.ToString();
socket = await ConnectToProxy(snirequest, hostName);
System.IO.Stream streamOut = socket.OutputStream.AsStreamForWrite();
StreamWriter writer = new StreamWriter(streamOut);
HostName destHost = new HostName(hostName);
IInputStream inputStream = socket.InputStream;
//Debug.WriteLine(hostName);
//Debug.WriteLine(uriReqStr);
//Debug.WriteLine(reqUri);
//Debug.WriteLine(orgRequest.ToString());
String testreq = getSniRequest(reqUri, orgRequest.ToString(), hostName, uriReqStr);
if (reqUri.StartsWith("https"))
{
snirequest = testreq;
await upgradeDatamiSocket(socket, hostName, snirequest, uriReqStr);
}
StreamSocket writeSocket = args.Socket;
await ReadSocketData(inputStream, writeSocket);
socket.Dispose(); // Dispose the socket after reading data from remote proxy
Debug.WriteLine("Socket disposed");
//await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(5));
}
}
catch (Exception ex)
{
Debug.WriteLine("Listener task Exception" + ex.Message);
}
}
推荐答案
这篇关于如何检查send-async是否发送http请求并由套接字流侦听器接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!