2017-01-30 135 views
0

我在curl比較新,我想在C中使用curl發送字符串。另一方面,作爲服務器,我有一個使用HttpServlet監聽HTTP請求的Java程序。 當我用curl發送一些數據時,我發現服務器獲取它,但沒有發送的格式。 例如,如果我送數據: 「8D015678」 在服務器側請求內容將是:56,68,48,49,53,54,55,56 這裏是客戶機代碼(C):CURL:客戶端和服務器通信Windows c/java

int status = 0; 
char * ip_to_connect = "http://127.0.0.1:5004/unilateral 
const char * request = "8D015678"; 
status = curl_easy_setopt(curl, CURLOPT_URL, ip_to_connect); 

if (!status) 
{ 
    status = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback); 
} 
if (!status) 
{ 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(request)); 
} 
if (!status) 
{ 
    status = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request); 
} 
if (!status) 
{ 
    status = curl_easy_perform(curl); 
} 

這裏是服務器(JAVA)的解析器代碼:

System.out.println("[requestHandlerFactory] header is not Content"); 
    int contentLength = request.getContentLength(); 
    this.content = new byte[contentLength]; 
    try { 
     ServletInputStream input = request.getInputStream(); 
     int read = input.read(content, 0, contentLength); 
     System.out.println("[doPost] contentLength = " + contentLength + "data = " + content[0]); 

這是否意味着我需要從ASCI到符號轉換器? 我認爲捲曲數據是按原樣傳遞的?我錯過了什麼? 如果我想要我的數據,例如。 8D015678由服務器獲取爲8D015678我應該如何發送它? 請詳細解釋數據如何傳遞以及如何處理curl中的客戶端和接收這些請求的服務器 提前致謝。希望它很清楚

回答

0

InputStream.read(..)當流結束時返回-1,否則該值可以平凡地轉換爲char。返回值和錯誤代碼的這種組合是舊的,而且很不幸,今天的設計也會有所不同。只需將其轉換爲字符並進行處理即可。

的官方文檔是https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

http://www.tutorialspoint.com/java/io/inputstream_read.htm 的工作示例。

的實質是:

// new input stream created 
     is = new FileInputStream("C://test.txt"); 

     System.out.println("Characters printed:"); 

     // reads till the end of the stream 
     while((i=is.read())!=-1) 
     { 
      // converts integer to character 
      c=(char)i; 

      // prints character 
      System.out.print(c); 
     }