Der Titel wird wie folgt umgeschrieben: Die Verbindung zu 127.0.0.1:3446 wurde vom Zielcomputer aktiv abgelehnt
P粉497463473
2023-08-27 13:30:24
<p>Ich verwende die WCF4.0-Vorlage -REST. Ich versuche, eine Methode zum Hochladen von Dateien mithilfe eines Streams zu erstellen.</p>
<p>问题总是发生在</p>
<pre class="brush:php;toolbar:false;">Stream serverStream = request.GetRequestStream();</pre>
<p>流式传输类:</p>
<pre class="brush:php;toolbar:false;">Namespace LogicClass
{
öffentliche Klasse StreamClass: IStreamClass
{
public bool UploadFile(string filename, Stream fileStream)
{
versuchen
{
FileStream fileToupload = new FileStream(filename, FileMode.Create);
byte[] bytearray = neues Byte[10000];
int bytesRead, totalBytesRead = 0;
Tun
{
bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
}
Catch (Exception ex) { throw new Exception(ex.Message); }
return true;
}
}
}</pre>
<p>REST-Einstellungen:</p>
<pre class="brush:php;toolbar:false;">[WebInvoke(UriTemplate = "AddStream/{filename}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
public bool AddStream(string filename, System.IO.Stream fileStream)
{
LogicClass.FileComponent rest = new LogicClass.FileComponent();
return rest.AddStream(filename, fileStream);
}</pre>
<p>Windows 窗体项目:用于测试</p>
<pre class="brush:php;toolbar:false;">private void button24_Click(object sender, EventArgs e)
{
byte[] fileStream;
using (FileStream fs = new FileStream("E:stream.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
{
fileStream = neues Byte[fs.Length];
fs.Read(fileStream, 0, (int)fs.Length);
fs.Close();
fs.Dispose();
}
string baseAddress = "http://localhost:3446/File/AddStream/stream.txt";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);
request.Method = "POST";
request.ContentType = "text/plain";
Stream serverStream = request.GetRequestStream();
serverStream.Write(fileStream, 0, fileStream.Length);
serverStream.Close();
Verwenden von (HttpWebResponse Response = request.GetResponse() als HttpWebResponse)
{
int statusCode = (int)response.StatusCode;
StreamReader-Reader = new StreamReader(response.GetResponseStream());
}
}</pre>
<p>我已关闭防火墙和 Internet 连接,但错误仍然存在.Gibt es eine bessere Möglichkeit, die Upload-Methode zu testen? </p>
<p>Stack-Trace: </p>
<blockquote>
<p>At System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress Socket-Adresse)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception&Exception)</p>
</blockquote><p><br /></p>
您不必重新启动电脑。而是重新启动 IIS。
“主动拒绝”意味着当您尝试连接时主机发送了重置而不是确认。因此,这在您的代码中不是问题。防火墙阻止连接,或者托管服务的进程未侦听该端口。这可能是因为它根本没有运行或者因为它正在侦听不同的端口。
启动托管服务的进程后,请尝试使用
netstat -anb
(需要管理员权限)来验证它是否正在运行并侦听预期的端口。更新:在 Linux 上,您可能需要执行
netstat -anp
。