導航:首頁 > IDC知識 > c實現http伺服器

c實現http伺服器

發布時間:2020-11-12 05:11:17

1、如何用c語言實現http伺服器

//服務端簡易代碼如下:
#include <stdio.h>  
#include <stdlib.h>  
  
#include <err.h>  
#include <event.h>  
#include <evhttp.h>  
  
void http_handle(struct evhttp_request *req, void *arg); /*  HTTP Request Handle  */  
  
int main(){  
    struct evhttp *httpd;  
    event_init();  
    httpd = evhttp_start("0.0.0.0", 2345);  
    if (httpd == NULL) {  
        fprintf(stderr, "Error: Unable to listen on %s:%d ");  
        exit(1);      
    }     
    evhttp_set_timeout(httpd, 2000);  
    evhttp_set_gencb(httpd, http_handle, NULL);  
    event_dispatch();  
    evhttp_free(httpd);  
  
    return 0;  
}  
  
void http_handle(struct evhttp_request *req, void *arg){  
    struct evbuffer *buf;  
    buf = evbuffer_new();  
  
    /*  Response the client  */  
    evhttp_send_reply(req, HTTP_OK, "OK", buf);  
  
    //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED");  
  
    /*  Release the memory  */  
    evbuffer_free(buf);  
    fprintf(stderr,"Send  ");  
}

編譯:編譯時把libevent的類庫中的.so文件和.h文件連接 進來。

2、怎麼用C實現Http POST功能向Http伺服器上傳文件

文件可以使用sendfile直接過去
比如剛開始是報文頭部結束\r\n\r\n直接write就可以
然後文件數據可以直接sendfile處理,
也可以
#define BUFSIZE 8196
while(read(fd,buf,BUFSIZE)>0){
write(...);
}
CONTENT-LENGTH是報文頭結束\r\n\r\n之後的位元組總數

3、怎麼用C實現Http POST功能向Http伺服器上傳文件

super viewDidLoad];
webview.backgroundColor = [UIColor clearColor];
webview.scalesPageToFit =YES;
webview.delegate =self;
NSURL *url =[[NSURL alloc] initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[webview loadRequest:request];
}

4、如何用c語言實現基於http的webservice

package com.yun.test;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;
import com.cezanne.golden.user.Exception;
import com.cezanne.golden.user.UserManagerServiceProxy;
import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPException;
public class testWebService {
public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
//標識Web Service的具體路徑
String endpoint = "WebService服務地址";
// 創建 Service實例
Service service = new Service();
// 通過Service實例創建Call的實例
Call call = (Call) service.createCall();
//將Web Service的服務路徑加入到call實例之中.
call.setTargetEndpointAddress( new java.net.URL(endpoint) );//為Call設置服務的位置
// 由於需要認證,故需要設置調用的SOAP頭信息。
Name headerName = new PrefixedQName( new QName("發布的wsdl里的targetNamespace里的url", "string_itemName") );
org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName);
header.addTextNode( "blablabla" );
call.addHeader(header);

// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("發布的wsdl里的targetNamespace里的url", "SoapHeader");
// soapHeaderElement.setNamespaceURI("發布的wsdl里的targetNamespace里的url");
// try
// {
// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
// }
// catch (SOAPException e)
// {
// e.printStackTrace();
// }
// call.addHeader(soapHeaderElement);
//調用Web Service的方法
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("opName");
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName(", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName(", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName(", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
oper.setReturnType(new javax.xml.namespace.QName(", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("", "return"));
oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
oper.addFault(new org.apache.axis.description.FaultDesc(
new javax.xml.namespace.QName("發布的wsdl里的targetNamespace里的url", "Exception"),
"Exception",
new javax.xml.namespace.QName("發布的wsdl里的targetNamespace里的url", "Exception"),
true
));
call.setOperation( oper );
call.setOperationName(new javax.xml.namespace.QName("發布的wsdl里的targetNamespace里的url", "opName"));
//調用Web Service,傳入參數
String res = ( String ) call.invoke( new Object[]("arg0","arg1"));
System.out.println("===============");
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(getResult());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
}
}
}

5、C語言實現HTTP請求,單片機GPRS模塊訪問網頁讀取數據

你要先向主機發個http頭,然後伺服器就會發給你網頁相關內容。你的這套系統運行在什麼環境下?

6、如何通過 c/c++ 實現http請求

TCP連接伺服器80埠,發送HTTP報文。
報文格式詳見HTTP協議(HTTP1.1 RFC2616)

7、如何用c實現http post json

http是基於Socket通信的一種通信規約,post是http規約的一種功能,json是常用於字元串解釋型編程語言及一些腳本上用的對象格式。

8、設計一個linux c語言,Http協議的伺服器,用socket收發消息,簡單點,如何實現。求高手解答

去看一下《Advanced Linux Programming》這本書吧,第11章講的就是怎麼用C語言實現一Http伺服器。
這里有下載地址(英文的):
http://www.advancedlinuxprogramming.com/alp-folder
英文看起來不順的話可以上網找找有沒有中文版的這本書,應該叫Linux高級編程吧~~~

9、C語言連接HTTP伺服器後如何用POST交流

這個和具體的網頁有關系的,你可以用HttpWatch之類的抓包工具分析一個網頁的請求和返回。

然後就可以自己模仿相關的請求訪問該網頁了。

與c實現http伺服器相關的知識