gpt4 book ai didi

java - 使用http组件解析http请求服务器端

转载 作者:行者123 更新时间:2023-11-29 09:11:47 25 4
gpt4 key购买 nike

嗨,我正在自学 android 开发。首先,我使用 apache http utils 在 android 上实现了一个网络服务器,当浏览到 localhost:8080 时,它会提供一个登录页面。在模拟器上。

我想知道如何在 android 服务器端解析登录请求。此 Web 服务器根据请求的 Uri 动态提供内容。比如当提交登录页面时,表单将发布到/login.do ,关于 localhost:8080 .

我已经为该操作注册了一个处理程序,如下所示。我想知道是否有一种方法可以在我的处理程序中解析请求正文并为 html 提供服务因此,我在表单中只有一个字段要解析,即 password .我只能阅读请求 header 。

任何正确方向的想法或指示将不胜感激。谢谢

import java.io.BufferedInputStream;  
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;

import android.content.Context;

public class LoginHandler implements HttpRequestHandler {
private Context context = null;
String req;
public LoginHandler(Context context) {
this.context = context;
}

@Override
public void handle( final HttpRequest request, HttpResponse response,
HttpContext httpcontext) throws HttpException, IOException {
HttpEntity entity = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream ) throws IOException {
String resp;

OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
HeaderIterator it = request.headerIterator();
while (it.hasNext())
{
System.out.println(it.next());
}
if (validateUser()==true)
resp = "<html><head></head><body><h1>Home<h1><p>Success."+req+"</p></body></html>";

else{
resp="<html><head></head><body><h1>Home<h1><p>Login Failed.</p></body></html>";}
writer.write(resp);
writer.flush();
}


});
response.setHeader("Content-Type", "text/html");
response.setEntity(entity);

}
boolean validateUser(){
boolean valid=false;
//if valid valid=true else valid=false
return valid;
}



}

输出如下:

    08-15 15:54:22.112: I/Process(650): Sending signal. PID: 650 SIG: 9
08-15 15:54:27.532: I/HTTPSERVICE(662): Creating and starting httpService
08-15 15:54:27.542: I/Notification(662): Notification Shown
08-15 16:08:19.332: I/HTTPSERVICE(662): Destroying httpService
08-15 16:08:19.352: I/Notification(662): Notification Destroyed
08-15 16:20:14.852: I/HTTPSERVICE(690): Creating and starting httpService
08-15 16:20:14.862: I/Notification(690): Notification Shown
08-15 16:20:28.102: D/dalvikvm(690): GC freed 2292 objects / 135976 bytes in 157ms
08-15 16:20:28.872: I/Resources(690): Loaded time zone names for en_US in 3090ms.
08-15 16:20:28.882: I/global(690): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
08-15 16:20:35.302: I/System.out(690): Host: localhost:8080
08-15 16:20:35.382: I/System.out(690): Accept-Encoding: gzip
08-15 16:20:35.422: I/System.out(690): Referer: http://localhost:8080/
08-15 16:20:35.422: I/System.out(690): Accept-Language: en-US
08-15 16:20:35.442: I/System.out(690): User-Agent: Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; sdk Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17
08-15 16:20:35.472: I/System.out(690): Origin: http://localhost:8080
08-15 16:20:35.512: I/System.out(690): Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
08-15 16:20:35.533: I/System.out(690): Content-Type: application/x-www-form-urlencoded
08-15 16:20:35.562: I/System.out(690): Accept-Charset: utf-8, iso-8859-1,utf-16,*;q=0.7
08-15 16:20:35.599: I/System.out(690): Content-Length: 25

我在 apache 站点上找不到任何适用于我的案例的资源。是否可以直接从 url 读取请求体?

最佳答案

环顾四周后,我找到了解决方案。在 handle 方法中添加以下内容就可以了。感谢原始海报。 http://www.androiddevblog.net/android/a-bare-minimum-web-server-for-android-platform

            if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
Log.v("RequestBody", EntityUtils.toString(entity, "UTF-8"));
entity.consumeContent();
}
}

关于java - 使用http组件解析http请求服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com