HttpUrlConnection Post 到 Laravel - java.io.filenotfoundexception
HttpUrlConnection Post to Laravel - java.io.filenotfoundexception
我正在尝试使用 HttpUrlConnection post 到 Laravel。但是正如标题所暗示的那样,它正在抛出异常。
请检查我的代码,让我知道我哪里错了。
public String HttpUrlPost()
{
BufferedWriter writer;
BufferedReader reader;
StringBuilder sb= new StringBuilder();
String data;
try
{
URL url = new URL("http://192.168.0.104/laravel5/public/login");
Log.d(TAG,"URL is : " + url.toString());
data = URLEncoder.encode("username",HTTP.UTF_8) + "=" + URLEncoder.encode("me",HTTP.UTF_8);
data += "&" + URLEncoder.encode("password",HTTP.UTF_8) + "=" + URLEncoder.encode("admin",HTTP.UTF_8);
Log.d(TAG,"data is : " + data);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
OutputStream os = connection.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(os, HTTP.UTF_8));
writer.write(data);
writer.flush();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
writer.close();
reader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return sb.toString();
}
Laravel这边很简单。我只想检查 post returns 是否有任何内容。
Route::post('login',function(){
$users = array('username' => 'a', 'password'=>'d');
return $users;});
请注意,我可以通过 laravel(它 returns Json)轻松使用 Get 方法。
而不是禁用 App\Http\Middleware\VerifyCsrfToken。php 您可以尝试将文件更新为:
class VerifyCsrfToken extends BaseVerifier {
private $openRoutes =
[
'public/login',
];
public function handle($request, Closure $next)
{
foreach($this->openRoutes as $route)
{
if ($request->is($route))
{
return $next($request);
}
}
return parent::handle($request, $next);
}
};
这允许您明确绕过您不想验证的特定路由,而无需全局禁用 csrf 验证。我用它来 ajax 通话。
我正在尝试使用 HttpUrlConnection post 到 Laravel。但是正如标题所暗示的那样,它正在抛出异常。
请检查我的代码,让我知道我哪里错了。
public String HttpUrlPost()
{
BufferedWriter writer;
BufferedReader reader;
StringBuilder sb= new StringBuilder();
String data;
try
{
URL url = new URL("http://192.168.0.104/laravel5/public/login");
Log.d(TAG,"URL is : " + url.toString());
data = URLEncoder.encode("username",HTTP.UTF_8) + "=" + URLEncoder.encode("me",HTTP.UTF_8);
data += "&" + URLEncoder.encode("password",HTTP.UTF_8) + "=" + URLEncoder.encode("admin",HTTP.UTF_8);
Log.d(TAG,"data is : " + data);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
OutputStream os = connection.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(os, HTTP.UTF_8));
writer.write(data);
writer.flush();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
writer.close();
reader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return sb.toString();
}
Laravel这边很简单。我只想检查 post returns 是否有任何内容。
Route::post('login',function(){
$users = array('username' => 'a', 'password'=>'d');
return $users;});
请注意,我可以通过 laravel(它 returns Json)轻松使用 Get 方法。
而不是禁用 App\Http\Middleware\VerifyCsrfToken。php 您可以尝试将文件更新为:
class VerifyCsrfToken extends BaseVerifier {
private $openRoutes =
[
'public/login',
];
public function handle($request, Closure $next)
{
foreach($this->openRoutes as $route)
{
if ($request->is($route))
{
return $next($request);
}
}
return parent::handle($request, $next);
}
};
这允许您明确绕过您不想验证的特定路由,而无需全局禁用 csrf 验证。我用它来 ajax 通话。