- 论坛徽章:
- 0
|
我想向一个HTTPS服务器提交一个表单,但是在调用SSL_write这个函数的时候出错了。
各位帮忙看一下,谢谢。
- static char *REQUEST =
- "POST /SSL/user/PasswordUserLogin HTTP/1.1\r\n"
- "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*\r\n"
- "Referer: [url]https://192.168.3.229/SSL/user/PasswordLogin.jsp[/url]\r\n"
- "Accept-Language: zh-cn\r\n"
- "Content-Type: application/x-www-form-urlencoded\r\n"
- "UA-CPU: x86\r\n"
- "Accept-Encoding: gzip, deflate\r\n"
- "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; MAXTHON 2.0)\r\n"
- "Host: 192.168.3.229\r\n"
- "Content-Length: 132\r\n"
- "Connection: Keep-Alive\r\n"
- "Cache-Control: no-cache\r\n\r\n"
- "Host=192.168.3.229&LoginName=cXV4aWFueWFuZw%3D%3D&LoginPassword=cXV4aWFueWFuZw%3D%3D&LoginNameX=quxianyang&LoginPasswordX=quxianyang\r\n";
- static char *host;
- static int port;
- static int http_request(SSL *ssl)
- {
- char *request = 0;
- char buf[BUFSIZZ];
- int r;
- int len,request_len;
- FILE *fw1;
-
- request_len = strlen(REQUEST);
- request = (char *)malloc(request_len);
- snprintf(request,request_len,REQUEST);
-
- printf("request1:%s",request);
- r = SSL_write(ssl,request,request_len);
- switch(SSL_get_error(ssl,r)){
- case SSL_ERROR_NONE:
- if(request_len != r)
- err_exit("Incomplete write!");
- break;
- default:
- berr_exit("SSL write problem!");
- }
- if((fw1 = fopen("response1.txt","w")) == NULL)
- return 1;
- while(1){
- r = SSL_read(ssl,buf,BUFSIZZ);
- switch(SSL_get_error(ssl,r)){
- case SSL_ERROR_NONE:
- len = r;
- break;
- case SSL_ERROR_WANT_READ:
- goto done;
- case SSL_ERROR_ZERO_RETURN:
- goto done;
- case SSL_ERROR_SYSCALL:
- fprintf(stderr,"SSL Error:Premature close\n");
- goto done;
- default:
- berr_exit("SSL read problem");
- }
- len = r;
- fwrite(buf,1,len,fw1);
- }
- done:
- fclose(fw1);
- free(request);
- return 0;
- }
- int main(int argc,char *argv[])
- {
- SSL_CTX *ctx;
- SSL *ssl;
- BIO *sbio;
- int sock;
- extern char *optarg;
- int c;
- SSL_SESSION *sess;
- int r;
-
- while((c=getopt(argc,argv,"h:p:ia:r"))!=-1){
- switch(c){
- case 'h':
- if(!(host=strdup(optarg)))
- err_exit("Out of memory");
- break;
- case 'p':
- if(!(port=atoi(optarg)))
- err_exit("Bogus port specified");
- break;
- }
- }
- /* Build our SSL context*/
- ctx=initialize_ctx(KEYFILE,PASSWORD);
- /* Connect the TCP socket*/
- sock=tcp_connect(host,port);
- /* Connect the SSL socket */
- ssl=SSL_new(ctx);
- SSL_set_fd(ssl,sock);
- sbio=BIO_new_socket(sock,BIO_NOCLOSE);
- if(SSL_connect(ssl)<=0)
- berr_exit("SSL connect error");
-
- /* Now make our HTTP request */
- //http_request(ssl);
- http_request(ssl);
-
- SSL_free(ssl);
- r=SSL_shutdown(ssl);
- switch(r){
- case 1:
- break; /* Success */
- case 0:
- case -1:
- default:
- berr_exit("Shutdown failed");
- }
- /* Shutdown the socket */
- destroy_ctx(ctx);
- close(sock);
- exit(0);
- }
复制代码
其中REQUEST 中的“cXV4aWFueWFuZw”是“quxianyang”的Base64编码后的字符串。
调用SSL_write后打印出了"SSL write problem!",我获取错误信息时得到的又是error=null,实在不知道是哪里错了。
我这个SSL的初始化应该没有错误,因为我试过用GET请求获取一个页面,是可以成功的。 |
|