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

2023-09-11 12:38:17 作者:水里有怪人

我要创建与CommuniGate Pro的服务器工作的移动应用程序。

例如,我需要做下面的Andr​​oid客户端 C - CGP服务器取值谈话并获得 XIMSS.nonce 节点值:

  C:GET / ximsslogin / HTTP / 1.1
  主持人:myserver.com
  内容类型:text / xml的
  内容长度:42

  < XIMSS>< listFeatures ID =列表/>< XIMSS>

S:HTTP / 1.1 200 OK
  内容长度:231
  连接:保持活动
  内容类型:text / xml的,字符集= UTF-8
  服务器:CommuniGatePro / 5.3

  <XIMSS><nonce>2C3E575E5498CE63574D40F18D00C873</nonce><language>german</language><response ID =S/&GT;&LT; / XIMSS&GT;
 

例如,在ActionScript 3.0,它看起来是这样的:

  VAR装载机:装载机=新的Loader();
loader.addEventListener(引发Event.COMPLETE,在completeHandler);
VAR的URLRequest:的URLRequest =新的URLRequest(...);
urlRequest.method = ...;
请将URLRequest.data = ...;
loader.load(的URLRequest);

私有函数在completeHandler(...):无效{...};
 
BS CS,Http1.0和1.1,get和post请求,响应状态码

它会怎么看Java中的的Andr​​oid 2.1

解决方案

随着Schnapple说,你的问题似乎非常广泛,是混乱的阅读和理解。

下面是一些一般性的code发送一个HTTP POST和GET从服务器的响应尽管这可能会有帮助。

 公共字符串postPage(字符串URL,文件数据,布尔returnAddr){

    RET = NULL;

    httpClient.getParams()的setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109)。

    httpPost =新HttpPost(URL);
    响应=无效;

    FileEntity TMP = NULL;

    TMP =新FileEntity(数据,UTF-8);

    httpPost.setEntity(TMP);

    尝试 {
        响应= httpClient.execute(httpPost,localContext);
    }赶上(ClientProtocolException E){
        的System.out.println(HTTPHelp:ClientProtocolException:+ E);
    }赶上(IOException异常E){
        的System.out.println(HTTPHelp:IOException异常:+ E);
    }
            。RET = response.getStatusLine()的toString();

            返回RET;
}
 

I'm going to create mobile application that works with CommuniGate Pro server.

For example, I need to make the following Android Client C - CGP Server S conversation and get XIMSS.nonce node value:

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

  <XIMSS><listFeatures id="list" /><XIMSS>

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

  <XIMSS><nonce>2C3E575E5498CE63574D40F18D00C873</nonce><language>german</language><response id="s"/></XIMSS> 

Example, in ActionScript 3.0 it looks this way:

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 { ... };

How will it look in Java for Android 2.1?

解决方案

As Schnapple says your question seems very broad and is confusing to read and understand.

Here is some general code to send a HTTP POST and get a response from a server though that may be helpful.

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;
}