search

Home  >  Q&A  >  body text

The title is rewritten as: Connection to 127.0.0.1:3446 was actively refused by the target machine

<p>I'm using WCF4.0 template -REST. I'm trying to create a method to upload files using a stream.</p> <p>问题总是发生在</p> <pre class="brush:php;toolbar:false;">Stream serverStream = request.GetRequestStream();</pre> <p>流式传输类:</p> <pre class="brush:php;toolbar:false;">namespace LogicClass { public class StreamClass : IStreamClass { public bool UploadFile(string filename, Stream fileStream) { try { FileStream fileToupload = new FileStream(filename, FileMode.Create); byte[] bytearray = new byte[10000]; int bytesRead, totalBytesRead = 0; do { 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 项目:</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 = new 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(); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { int statusCode = (int)response.StatusCode; StreamReader reader = new StreamReader(response.GetResponseStream()); } }</pre> <p>我已关闭防火墙和 Internet 连接,但错误仍然存在。Is there a better way to test the upload method? </p> <p>Stack trace: </p> <blockquote> <p>At System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socket address) 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>
P粉497463473P粉497463473505 days ago672

reply all(2)I'll reply

  • P粉254077747

    P粉2540777472023-08-28 12:21:14

    You do not have to restart your computer. Instead, restart IIS.

    reply
    0
  • P粉270842688

    P粉2708426882023-08-28 09:47:01

    "Active Rejection" means that the host sent a reset instead of an acknowledgment when you tried to connect. So this is not a problem in your code. A firewall is blocking the connection, or the process hosting the service is not listening on the port. This could be because it's not running at all or because it's listening on a different port.

    After starting the process hosting the service, try using netstat -anb (requires administrator privileges) to verify that it is running and listening on the expected port.

    Update: On Linux, you may need to execute netstat -anp.

    reply
    0
  • Cancelreply