1. 程式人生 > >HTTP傳送RAW請求注意的問題

HTTP傳送RAW請求注意的問題

1.使用jar

httpcomponents-client-4.5.6-bin.tar.gz

解壓後倒入lib中的所有包

 

2.參考程式碼如下

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;


import org.apache.http.util.EntityUtils;

 


public class HttpClientTest {
public static void main (String args[]) throws ClientProtocolException, IOException {
String url="http://**********/tppservice/es/zytWtService";
String outputStr="{\"head\":{\"userId\":\"\",\"subsystem\":\"ZYT_APP\",\"subject\":\"ZYT_WT_001\",\"token\":\"TPP_ZYT\",\"sign\":\"d5682c3f2bede511d3f0b1fac3598454\",\"serialNo\":\"1412660\",\"transDate\":\"2018-11-02 16:09:20\"},\"busi\":{\"flag\":\"1\",\"pwd\":\"www123456\",\"userName\":\"18500000058\"}}";
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
StringEntity postingString = new StringEntity(outputStr);// json傳遞
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
String content = EntityUtils.toString(response.getEntity());
System.out.println(content);
}
}

3.包導的不對會報NoClassDefFoundError: org.apache.commons.logging.LogFactory異常,是依賴的包沒有導完整