Flutter Dio请求Laravel出现Bad hostname NotFoundHttpException排查
问题:Flutter Dio提交数据触发"Bad hostname provided"的NotFoundHttpException,但Postman请求正常
我本地用Flutter的Dio包提交数据保存时,触发了带“Bad hostname provided”提示的NotFoundHttpException异常,但用Postman发起完全相同的请求却一切正常。试过多种排查方法都没解决,想知道这个异常的产生原因。
后端代码(Laravel控制器方法)
public function store(StoreSaleRequest $request) { $waybillNumber = GenerateUniqueId::create('sales', 'FMC', $request->customer_type === 'regular' ? 'R' : 'D', 7, true); $customer = $this->createOrUpdateCustomer($request); $sale = Sale::create([ 'customer_id' => $customer->id, 'waybill_number' => $waybillNumber, 'amount_paid' => $request->amount_paid, 'cod' => $request->cod, 'date' => $request->date ?? now(), 'total_price' => $request->total_price, 'delivery_fee' => $request->delivery_fee, 'subtotal' => $request->subtotal, 'transaction_id' => $request->transaction_id, 'customer_type' => $request->customer_type, 'discount' => $request->discount, 'payment_type' => $request->payment_type, ]); $sale->salesItems()->createMany($request->items); $this->updateStock($customer->id, $request->items); if ($sale->customer) { SmsJob::dispatch( config('services.default_sms'), $sale->customer->phone, "message", ) ->delay(now()->addSeconds(20)); } $sale->load('salesItems'); return response()->json([ 'status' => true, 'message' => 'Sale saved successfully', ]); }
错误信息
message: Bad hostname provided., exception: Symfony\Component\HttpKernel\Exception\NotFoundHttpException, file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php, line: 414, trace: [{file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php, line: 384, function: prepareException, class: Illuminate\Foundation\Exceptions\Handler, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php, line: 51, function: render, class: Illuminate\Foundation\Exceptions\Handler, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php, line: 185, function: handleException, class: Illuminate\Routing\Pipeline, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php, line: 39, function: Illuminate\Pipeline\{closure}, class: Illuminate\Pipeline\Pipeline, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php, line: 180, function: handle, class: Illuminate\Http\Middleware\TrustProxies, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php, line: 116, function: Illuminate\Pipeline\{closure}, class: Illuminate\Pipeline\Pipeline, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php, line: 175, function: then, class: Illuminate\Pipeline\Pipeline, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php, line: 144, function: sendRequestThroughRouter, class: Illuminate\Foundation\Http\Kernel, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/public/index.php, line: 51, function: handle, class: Illuminate\Foundation\Http\Kernel, type: ->}, {file: /Users/okmans/Projects/FM Cosmetics/app/erp project/sports/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php, line: 16, function: require_once}]}
异常产生原因分析
这个异常的根源不在提交保存请求的逻辑里,而是后端执行SmsJob短信发送任务时,配置的短信服务域名/主机地址无效:
- 请求链路差异:Postman仅触发了保存接口的同步逻辑,而Flutter请求在保存成功后,后端会异步执行
SmsJob。该Job调用config('services.default_sms')对应的短信服务时,配置的主机地址格式错误(比如含空格、特殊字符,或是未解析的无效域名),导致HttpClient发起请求时抛出"Bad hostname provided"错误。 - 异常包装传递:Laravel的异常处理器将短信请求的异常包装成
NotFoundHttpException返回,造成了“提交保存请求出错”的错觉。可以通过以下方式验证:- 检查
config/services.php中default_sms的配置项,确认主机地址是否正确 - 查看
SmsJob的具体实现,排查是否使用了错误的hostname - 临时注释
SmsJob::dispatch()代码,再用Flutter发起请求,若不再报错,即可确认问题出在短信任务上
- 检查
内容的提问来源于stack exchange,提问作者Leslie Joe
相关产品推荐
相关产品推荐

