高山听流水
2018-11-08 04:47
采纳率: 50%
浏览 1.6k

asp.net接收post数据报错:由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

我用asp.net写了一个接入其它网站登录api的网页,放到我的服务器上成功了,但放到学校的服务器上接收post过来的token的时候报了下面的错误:

Server Error in '/classMeeting' Application.

由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 112.65.235.59:443
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 112.65.235.59:443

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SocketException (0x274c): 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 112.65.235.59:443]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +309
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +633

[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) +708
System.Net.HttpWebRequest.GetRequestStream() +21
classMeeting.index.GetAccess_token(String code, String client_id, String redirect_uri, String client_secret) in C:\Users\admin\source\repos\classMeeting\index.aspx.cs:130
classMeeting.index.readyLogin() in C:\Users\admin\source\repos\classMeeting\index.aspx.cs:32
classMeeting.index.Page_Load(Object sender, EventArgs e) in C:\Users\admin\source\repos\classMeeting\index.aspx.cs:25
System.Web.UI.Control.OnLoad(EventArgs e) +109
System.Web.UI.Control.LoadRecursive() +68
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3321

我的源码是:

    public static string GetAccess_token(string code, string client_id, string redirect_uri, string client_secret)
        {
            var url = "https://openapi.yiban.cn/oauth/access_token";
            byte[] byteArray = Encoding.UTF8.GetBytes(string.Format("client_id={0}&client_secret={1}&code={2}&redirect_uri={3}", client_id, client_secret, code, redirect_uri));
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
            webRequest.Method = "Post";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
            ServicePointManager.DefaultConnectionLimit = 50;
            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.Default);
            return php.ReadToEnd();
        }

应该怎么解决呢,求大佬

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 高山听流水 2018-12-21 08:42
    已采纳

    解决了,问题是服务器的外网访问被禁用了,服务器要向别的主机发送和接受web请求的时候必须保证在服务器上能访问接受web请求并对此做出相应的主机。这个可能是服务器禁用了外网访问的80接口或者443端口,也有可能是服务器的云控制端禁用了该服务器的访问外网的权限。

    已采纳该答案
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题