如何在Java中通过HTTP get发送数据?

如何在Java中通过HTTP get发送数据?,http,get,libcurl,multiplayer,cocos2d-x,Http,Get,Libcurl,Multiplayer,Cocos2d X,因此,我将通过iphone连接到servlet并使用HTTP。我正在开发一款多人游戏,我想知道如何通过HTTP get in java(doGet-in-java)向iphone发送特定数据。我在iphone上使用libcurl(cocos2d-x) 下面是如何设置我的代码: size_t write_data(void* buffer, size_t size, size_t nmemb, void *data) { //do stuff with data } //

因此,我将通过iphone连接到servlet并使用HTTP。我正在开发一款多人游戏,我想知道如何通过HTTP get in java(doGet-in-java)向iphone发送特定数据。我在iphone上使用libcurl(cocos2d-x)

下面是如何设置我的代码:

size_t write_data(void* buffer, size_t size, size_t nmemb, void *data)
{
    //do stuff with data        
}

//main or some init method
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
    char *data = "hi imma get=yeah";
    curl_easy_setopt(curl, CURLOPT_URL, "http://whatever.com");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 

    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
    {
              CCLOG("WELP BETTER DO SOMETHING ERROR");
    }

    curl_easy_cleanup(curl);
}
所以,我想知道的是如何使用java中doGet方法中的响应向上面定义的write_函数发送字符串?如中所示,在doGet方法中如何处理响应参数

以下是doGet方法供参考:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws    ServletException, IOException
 {
    System.out.println("GET METHOD CALLED");

 }
那么,现在我该如何处理这个响应来向write_函数传递一些数据呢


感谢您的所有意见

使用响应的编写器,如下所示

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
{
    // tell response what format your output is in, we select plain text here
    response.setContentType("text/plain;charset=UTF-8");
    // ask the response object for a Writer object
    PrintWriter out = response.getWriter();
    try {
        // and use it like you would use System.out.  Only, this stuff gets sent 
        //to the client
        out.println("GET METHOD CALLED");
    } finally {
        // housekeeping: ensure that the Writer is closed when you're ready.
        out.close();
    }
}
在某些情况下,使用流更容易。这也是可能的,但是你永远不能同时打开Writer和OutputStream

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
{
    response.setContentType("text/plain;charset=UTF-8");
    // ask the response object for an OutputStream object
    OutputStream os = response.getOutputStream();

    try {
        // output some stuff, here just the characters ABC
        os.write(new byte[]{65,66,67});
    } finally {            
        os.close();
    }

}

如果您想了解更多,web上提供了大量关于servlet的教程,包括使用response's Writer编写的

,如下所示

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
{
    // tell response what format your output is in, we select plain text here
    response.setContentType("text/plain;charset=UTF-8");
    // ask the response object for a Writer object
    PrintWriter out = response.getWriter();
    try {
        // and use it like you would use System.out.  Only, this stuff gets sent 
        //to the client
        out.println("GET METHOD CALLED");
    } finally {
        // housekeeping: ensure that the Writer is closed when you're ready.
        out.close();
    }
}
在某些情况下,使用流更容易。这也是可能的,但是你永远不能同时打开Writer和OutputStream

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
{
    response.setContentType("text/plain;charset=UTF-8");
    // ask the response object for an OutputStream object
    OutputStream os = response.getOutputStream();

    try {
        // output some stuff, here just the characters ABC
        os.write(new byte[]{65,66,67});
    } finally {            
        os.close();
    }

}

如果您想了解更多,web上有大量关于servlet的教程,包括

好的,谢谢!write(String)几乎可以将任何写入的字符串发送到客户端。这是否意味着void*数据将包含我使用PrintWriter或OutputStream写入的字符串?@dGetUser在您的示例中,servlet发送的数据将移交给write_数据回调函数。在您的示例中,我没有看到
void*data
,而且从未使用过
char*data
。void*data在write_函数中,好的,谢谢!!是的,char*数据将用于POST方法,忘记删除它了。好的,谢谢!write(String)几乎可以将任何写入的字符串发送到客户端。这是否意味着void*数据将包含我使用PrintWriter或OutputStream写入的字符串?@dGetUser在您的示例中,servlet发送的数据将移交给write_数据回调函数。在您的示例中,我没有看到
void*data
,而且从未使用过
char*data
。void*data在write_函数中,好的,谢谢!!是的,char*数据将在POST方法中使用,但忘了删除它。