【问题标题】:Java HTTP webbit how to send/receive messageJava HTTP webbit如何发送/接收消息
【发布时间】:2013-08-15 08:07:34
【问题描述】:

我有一个服务器和一个客户端,
在服务器端我有这个处理程序

 @Override
 public void handleHttpRequest(HttpRequest httpRequest,
 HttpResponse httpResponse,
 HttpControl httpControl) throws Exception {
 // ..
 }

问题是如何从客户端发送数据,服务器端的什么方法会包含发送的数据?

如果有更好的使用webbit进行通信的方法,也将受到欢迎。

【问题讨论】:

  • 好的,看我的回答:D

标签: java http client-server


【解决方案1】:

在 POST 请求中,参数作为请求正文发送,位于请求头之后。

要使用 HttpURLConnection 进行 POST,您需要在打开连接后将参数写入连接。

此代码应该可以帮助您入门:

String urlParameters = "param1=a&param2=b&param3=c";
String request = "http://example.com/index.php";
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();           
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);

DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();

或者,您可以使用此帮助程序发送 POST 请求并获取请求

    public static String getStringContent(String uri, String postData, 
    HashMap<String, String> headers) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost();
    request.setURI(new URI(uri));
    request.setEntity(new StringEntity(postData));
    for(Entry<String, String> s : headers.entrySet())
    {
        request.setHeader(s.getKey(), s.getValue());
    }
    HttpResponse response = client.execute(request);

    InputStream ips  = response.getEntity().getContent();
    BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
    if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK)
    {
        throw new Exception(response.getStatusLine().getReasonPhrase());
    }
    StringBuilder sb = new StringBuilder();
    String s;
    while(true )
    {
        s = buf.readLine();
        if(s==null || s.length()==0)
            break;
        sb.append(s);

    }
    buf.close();
    ips.close();
    return sb.toString();
   }

【讨论】:

  • 所以问题是......在我写在我的问题中的处理函数中,我使用 POST 请求发送的数据到底在哪里?
  • 对于第一个例子,它的urlParameters,对于后者,它通过参数传递。可以吗?
  • 不,我的意思是在这个函数中 public void handleHttpRequest(HttpRequest httpRequest, HttpResponse httpResponse, HttpControl httpControl) 我在哪里可以找到数据?例如不应该是 httpRequest.body() 吗?因为我试过了,没用
  • 我给你的代码会给你响应,所以你不需要那个:)
  • 但我也负责服务器端:D,这实际上是主要问题,因为我能够向服务器发送一些数据但我不知道如何处理这些数据并正确响应。
【解决方案2】:

通常会扩展 HttpServlet 并覆盖 doGet。

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServlet.html

我不熟悉 webbit,但我不认为它是一个 Servlet 网络服务器。它提到它提供静态页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多