当前位置:  开发笔记 > 编程语言 > 正文

如何发送HTTP POST请求和接收响应?

如何解决《如何发送HTTPPOST请求和接收响应?》经验,为你挑选了1个好方法。

我将创建与CommuniGate Pro服务器一起使用的移动应用程序.

例如,我需要进行以下Android客户端C- CGP服务器S对话并获取XIMSS.nonce节点值:

C:GET /ximsslogin/ HTTP/1.1
  Host: myserver.com
  Content-Type: text/xml
  Content-Length: 42

  

S:HTTP/1.1 200 OK
  Content-Length: 231
  Connection: keep-alive
  Content-Type: text/xml;charset=utf-8
  Server: CommuniGatePro/5.3

  2C3E575E5498CE63574D40F18D00C873german 

例如,在ActionScript 3.0中它看起来像这样:

var loader:Loader = new Loader();
loader.addEventListener(Event.COMPLETE, completeHandler);
var urlRequest:URLRequest = new URLRequest(...);
urlRequest.method = ...;
urlRequest.data = ...;
loader.load(urlRequest);

private function completeHandler(...):void { ... };

如何在Java for Android 2.1中看到它?



1> Donal Raffer..:

正如Schnapple所说,你的问题看起来非常广泛,并且令人难以阅读和理解.

下面是一些发送HTTP POST并从服务器获取响应的通用代码,虽然这可能会有所帮助.


public String postPage(String url, File data, boolean returnAddr) {

    ret = null;

    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

    httpPost = new HttpPost(url);
    response = null;

    FileEntity tmp = null;       

    tmp = new FileEntity(data,"UTF-8");

    httpPost.setEntity(tmp);

    try {
        response = httpClient.execute(httpPost,localContext);
    } catch (ClientProtocolException e) {
        System.out.println("HTTPHelp : ClientProtocolException : "+e);
    } catch (IOException e) {
        System.out.println("HTTPHelp : IOException : "+e);
    } 
            ret = response.getStatusLine().toString();

            return ret;
}

推荐阅读
Chloemw
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有