ユーザーをサインインおよびサインアウトできるレールアプリがあり、この機能はdeviseを使用して実装されています。また、サインインしてレールアプリデータベースから情報を取得するために使用したいJavaデスクトップアプリケーションもあります。私がレールに持っているモデルはウェブと呼ばれます。rake routes
次の出力が得られます。
new_web_session GET /webs/sign_in(.:format) webs/sessions#new
web_session POST /webs/sign_in(.:format) webs/sessions#create
だから、これまでのところ、Apache httpcomponents が Java アプリケーションで使用されていることがわかりました。私はこれを試しました:
public class Connectweb {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
// TODO code application logic here
Connectweb connect = new Connectweb();
System.out.println("\nTesting 2 - Send Http POST request");
connect.sendPost();
}
private void sendPost() throws Exception {
String url = "http://localhost:3000/webs/sign_in";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// add header
post.setHeader("User-Agent", USER_AGENT);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("utf8", Character.toString('\u2713')));
nameValuePairs.add(new BasicNameValuePair("web[email]", "email@email.com"));
nameValuePairs.add(new BasicNameValuePair("web[password]", "password"));
nameValuePairs.add(new BasicNameValuePair("commit", "Sign in"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
これにより、Web ページが/webs/sign_in
出力として返されます。
Sending 'POST' request to URL :http://example.com/webs/sign_in
Post parameters : org.apache.http.client.entity.UrlEncodedFormEntity@3173175
Response Code : 200
<!DOCTYPE html>
<html>
<head>
<meta content="Sign In in XOMR" name="description" />
<meta content="log in, access account" name="keywords" />
<title>XOMR | Sign in</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap-fileupload/bootstrap-fileupload.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap-fileupload/bootstrap-fileupload.min.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/exam_results.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/exams.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/members.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/orgs.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/qr_codes.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/static_pages.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/users.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-fileupload/bootstrap-fileupload.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-fileupload/bootstrap-fileupload.min.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-typeahead/bootstrap-typeahead.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-typeahead/bootstrap-typeahead.min.js?body=1" type="text/javascript"></script>
<script src="/assets/exam_results.js?body=1" type="text/javascript"></script>
<script src="/assets/exams.js?body=1" type="text/javascript"></script>
<script src="/assets/members.js?body=1" type="text/javascript"></script>
<script src="/assets/orgs.js?body=1" type="text/javascript"></script>
<script src="/assets/qr_codes.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-transition.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-affix.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-alert.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-button.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-carousel.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-collapse.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-dropdown.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-modal.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-scrollspy.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-tab.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-tooltip.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap-popover.js?body=1" type="text/javascript"></script>
<script src="/assets/static_pages.js?body=1" type="text/javascript"></script>
<script src="/assets/users.js?body=1" type="text/javascript"></script>
<script src="/assets/web_registrations.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<link href="/assets/favicon.png" rel="shortcut icon" type="image/vnd.microsoft.icon" />
<meta content="authenticity_token" name="csrf-param" />
<meta content="o2QDfH4tjiOkLAXIIIzX5Nxni2ZtQUj/6ws6sKnwt6I=" name="csrf-token" />
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target=".bs-docs-sidebar">
<header class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/" class="brand">XOMR</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="/help">Help</a></li>
</ul>
<form accept-charset="UTF-8" action="/webs/sign_in" class="pull-right navbar-form" id="new_web" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="o2QDfH4tjiOkLAXIIIzX5Nxni2ZtQUj/6ws6sKnwt6I=" /></div>
<table>
<tbody>
<tr>
<td><input class="span2" id="web_email" name="web[email]" placeholder="Email" required="required" size="30" type="email" value="sushilthe@gmail.com" /></td>
<td><input class="span2" id="web_password" name="web[password]" placeholder="Password" required="required" size="30" type="password" /></td>
<td><input class="btn" name="commit" type="submit" value="Sign in" /></td>
</tr>
<tr>
<td>
<label class="checkbox">
<input name="web[remember_me]" type="hidden" value="0" /><input id="web_remember_me" name="web[remember_me]" type="checkbox" value="1" /> <label for="web_remember_me">Remember me</label>
</label>
</td>
<td><small> <a href="/webs/password/new">Forgot your password?</a></small></td>
</tr>
</tbody>
</table>
</form> </div><!--/.nav-collapse -->
</div>
</div>
</header>
<header class="jumbotron subhead" id="overview">
<div class="container">
<h1>Sign In</h1>
<p class="lead"> Log in to access your information. </p>
</div>
</header>
<div class = "container">
<section id = "forms">
<form accept-charset="UTF-8" action="/webs/sign_in" class="form-horizontal, left" id="new_web" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="o2QDfH4tjiOkLAXIIIzX5Nxni2ZtQUj/6ws6sKnwt6I=" /></div>
<div class = "row-fluid">
<div class="control-group">
<div class="controls">
<input class="span6" id="web_email" name="web[email]" placeholder="Email" required="required" size="30" type="email" value="sushilthe@gmail.com" />
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="span6" id="web_password" name="web[password]" placeholder="Password" required="required" size="30" type="password" />
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input name="web[remember_me]" type="hidden" value="0" /><input id="web_remember_me" name="web[remember_me]" type="checkbox" value="1" /> <label for="web_remember_me">Remember me</label>
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-large btn-primary" name="commit" type="submit" value="Sign in" />
</div>
</div>
</div>
</form> <div class="btn-group">
<a href="/user/signup" class="btn">Sign up</a>
<a href="/webs/password/new" class="btn">Forgot your password?</a>
<a href="/webs/confirmation/new" class="btn">Didn't receive confirmation instructions?</a><br />
</div>
<br>
</section>
</div>
<div class="breadcrumb"><a href="/">Home</a> / <span class="active">Sign In</span></div>
<footer class="footer">
<div class="container-fluid">
<address>
<strong>The XOMR Project</strong><br>
P. O. Box 99999<br>
Kathmandu, Nepal<br>
Email: <a href="mailto:xomr.project@gmail.com">xomr.project@gmail.com</a>
</address>
</div>
</footer>
<pre class='debug_dump'>--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ! '?'
web: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
email: sushilthe@gmail.com
password: quartz
commit: submit
action: create
controller: webs/sessions
</pre>
</body>
</html>
助けてください。