C#应用程序在邮政请求期间悬挂到Python HTTP服务器
我有一个C#应用程序,该应用程序向Python创建的简单HTTP服务器提出了发布请求,但是我的请求从未“完成”,并且不会超越发出异步的POST请求。这是我从客户端(C#App)进行的呼叫:
private void sendPost(HttpClientAdaptor client, MyDataObject myDataObject) {
var payload = JsonConvert.SerializeObject(myDataObject);
var content = new StringContent(payload, Encoding.UTF8, "application/json");
try {
if (client.isDisposed) {
return;
}
var response = client?.PostAsync(ApiEndpoint, content); // this hangs forever
以及我用Python编写的HTTP服务器:
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
from io import BytesIO
class S(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b'This is POST request. ')
response.write(b'Received: ')
response.write(body)
self.wfile.write(response.getvalue())
def run(server_class=HTTPServer, handler_class=S, port=5000):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...\n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...\n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
我知道请求将其送到我的服务器,因为我使用了一些打印语句来打印有效载荷,但是我的客户端似乎从未确认过我的服务器的200响应。我已经验证了服务器正在运行,我没有混合端口,并且通过浏览器进行了GET请求。
我怀疑我的Python服务器出了问题,因此它没有“完成”交易,因此我的客户没有得到响应。
顺便说一句:是否有更简单的方法可以为我的客户端旋转HTTP服务器(用C#编写的Windows应用程序)?我只需要一种返回200个状态的方法。
I have a C# app that makes a post request to a simple http server created in Python but my request never "finishes" and doesn't progress past the point of making an asynchronous POST request. This is the call I'm making from my client (C# app):
private void sendPost(HttpClientAdaptor client, MyDataObject myDataObject) {
var payload = JsonConvert.SerializeObject(myDataObject);
var content = new StringContent(payload, Encoding.UTF8, "application/json");
try {
if (client.isDisposed) {
return;
}
var response = client?.PostAsync(ApiEndpoint, content); // this hangs forever
And my http server written in python:
#!/usr/bin/env python3
"""
Very simple HTTP server in python for logging requests
Usage::
./server.py [<port>]
"""
from http.server import BaseHTTPRequestHandler, HTTPServer
import logging
from io import BytesIO
class S(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b'This is POST request. ')
response.write(b'Received: ')
response.write(body)
self.wfile.write(response.getvalue())
def run(server_class=HTTPServer, handler_class=S, port=5000):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...\n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...\n')
if __name__ == '__main__':
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
I know the request is making it to my server since I used some print statements to print the payload but my client seemingly never acknowledges the 200 response from my server. I've verified the server is running, I'm not mixing up the port, and a GET request works via a browser.
I suspect something's wrong with my python server such that it's not 'finishing' the transaction and therefore my client doesn't get a response.
As an aside: Is there a more simple approach to spin up an http server for my client (windows app written in C#)? I just need a way to return a 200 status.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用异步功能,并且不等待它。
而不是
尝试
将您的方法签名
从
You are using an async function and are not awaiting it.
Instead of
try
And change your method signature from
To