ASIFormDataRequest实现post的代码示例

用jquery实现的Post方法可能如下

var param = $.param({
data: JSON.stringify({"from":"234","messageid":"32132123","to":"234","conversationid":"4123456","timestamp":1459000790138,"type":1,"content":"ew","imageurl":""})
});
$http({
url: MAIN_HOST_URL + '/chat/createmessage',
method: "POST",
data: param,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function (response) {
var data = response.data,
status = response.status,
header = response.header,
config = response.config;

//console.log(JSON.stringify(data));

if (status == 200) {

}
$ionicLoading.show({template: data.m, noBackdrop: true, duration: 2000});

scope.$broadcast('scroll.refreshComplete');
}, function (response) {
//console.log("error: " + JSON.stringify(response));
var data = response.data,
status = response.status,
header = response.header,
config = response.config;

//JSON.stringify(
scope.$broadcast('scroll.refreshComplete');
});

相同的代码,可以转换到ios里,使用ASIFormDataRequest实现,代码如下:


.xx.h

@interface ChatViewController : UIViewController<ASIHTTPRequestDelegate> {

}

xx.m

- (void) postMessage {

    NSString *s = [NSString stringWithFormat:@"http://%@/chat/createmessage", site_url];

 

    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:s]];

    

    NSString *datas = @"{\"from\":\"234\",\"messageid\":\"32132123\",\"to\":\"234\",\"conversationid\":\"4123456\",\"timestamp\":1459000790138,\"type\":1,\"content\":\"ew\",\"imageurl\":\"\"}";

 

    {

        NSError *error;

        //[request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded; encoding=utf-8"];

        //[request addRequestHeader:@"Accept" value:@"application/json"];

        //[request setRequestMethod:@"POST"];

        //[request setPostBody:tempJsonData];

        [request setPostValue:datas forKey:@"data"];

        [request startAsynchronous];

    }

}

 

- (void)requestFinished:(ASIHTTPRequest *)request

{

}

 

- (void)requestFailed:(ASIHTTPRequest *)request

{

    NSError *error = [request error];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient 是一个用于发送 HTTP 请求的库,它在许多编程语言中都有实现,如Java、Python等。在Java中,Apache HttpClient 是一个常用的库来进行HTTP请求,包括POST方法。以下是一个简单的Java代码示例,演示如何使用HttpClient执行POST请求: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientPostExample { public static void main(String[] args) throws Exception { // 创建HttpClient实例 CloseableHttpClient httpClient = HttpClients.createDefault(); try { // 创建HttpPost请求 HttpPost httpPost = new HttpPost("http://example.com/api/endpoint"); // 设置POST数据 String jsonPayload = "{\"key\":\"value\"}"; // 替换为你实际的JSON数据 StringEntity input = new StringEntity(jsonPayload, "application/json"); httpPost.setEntity(input); // 执行请求并获取响应 CloseableHttpResponse response = httpClient.execute(httpPost); // 检查状态码 int responseCode = response.getStatusLine().getStatusCode(); System.out.println("Response Code: " + responseCode); // 读取并打印响应体 HttpEntity entity = response.getEntity(); if (entity != null) { String responseBody = EntityUtils.toString(entity); System.out.println("Response Body: " + responseBody); } else { System.out.println("No response body."); } } finally { // 关闭资源 httpClient.close(); } } } ``` 在这个例子中,你需要替换`"http://example.com/api/endpoint"`为你要发送POST请求的实际URL,`jsonPayload`为POST的数据。请根据你的具体需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值