python3.7 flask2.2.3 webapi can be connected by Postman and browser but not c# Net6 HttpClient.PostAsync #5090
Replies: 1 comment
-
@superdavid0816 Check if the Flask API is running: Ensure that your Flask API is running and accessible. You mentioned that you can successfully connect to the API using Postman and a web browser, so make sure the API is still running when you try to access it from C#. Verify the API endpoint: Double-check the URL you are using to access the Flask API in your C# code. Ensure that the URL is correct, including the host, port, and any additional path or query parameters. Verify the network connectivity: Check if there are any network-related issues that could prevent the C# application from connecting to the Flask API. Make sure there are no firewalls or network configurations blocking the connection. Verify the Flask API's host and port: In your Flask code, you specified host='0.0.0.0' and port=58080. Confirm that these values match the host and port you are using in your C# code. Check if CORS is causing the issue: Since you have enabled CORS in your Flask API using the flask_cors package, ensure that it is configured correctly and allows requests from the domain or origin where your C# application is hosted. You may need to specify the allowed origins explicitly. Update C# code for HttpClient: Make sure you have the correct C# code to send the request using HttpClient.PostAsync. Here's an example: csharp class Program
} By following these steps, you should be able to troubleshoot the connection issue and successfully access your Flask API from your C# application. |
Beta Was this translation helpful? Give feedback.
-
Hi,
I build a very simple web api by flask2.2.3 using python3.7.
I can use Postman and browser to connect this webapi but not working in c# Net6 by HttpClient.PostAsync.
Please help me to pass this error.
Thank you.
error message in c#:
{System.Net.Sockets.SocketException (111): Connection refused
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.g__WaitForConnectWithCancellation|277_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)}
here is the code:
from flask import Flask, jsonify, redirect, url_for, render_template,request, Response
import json
from flask_cors import CORS
app = Flask(name)
CORS(app)
@app.route('/qryData/', methods=['POST', 'GET'])
def qryData():
if name == 'main':
app.run(host='0.0.0.0',port=58080,debug=True)
Thanks for your help.
Beta Was this translation helpful? Give feedback.
All reactions