Home > Software engineering >  ASP.NET receives post data issue
ASP.NET receives post data issue

Time:01-18

I used ASP.NET to write a web page that accesses other websites to log in to the API, and it was successfully placed on my server, but when it was placed on the school's server to receive the token from the post, the following error was reported:

Server Error in '/classMeeting' Application. A connection attempt failed because the connecting party did not properly reply after a period of time or the connected host became unresponsive. 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:

A connection attempt failed because the connected party did not properly reply after a period of time or the connected host became unresponsive. 112.65.235.59:443

Source Error:

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

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
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

My source code is:

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();
}

How should we do to fix this error?

CodePudding user response:

If you get any error code like 10036 for a socket connection error then you can find the related error section from this link

Check the parameters you use for connection like hostname and port, and also make sure your firewall doesn't drop packages or block your connection.

CodePudding user response:

The code you provided is correct, there are several reasons for this phenomenon:

  1. Check whether the status of the server is abnormal. For example, there may be a state of not starting or being paralyzed.
  2. Whether the status of your destination URL is normal. Whether there is an unreachable state.
  3. Whether your server can access your target website. Due to the server's need for a secure environment, there are usually some URLs that cannot be accessed by the server. At this point you need to change your server port. If it still doesn't work, try sending and receiving web requests to hosts that it can access successfully.
  • Related