I want to connect to http server using sockets on windows phone 7.5(MANGO). Code looks like this:
public class sockets
{
// Buffer for incoming data
private byte[] _receiveBuffer;
public void Demo()
{
String host = "209.85.148.106";
int port = 80;
String Message = "GET / HTTP/1.1 Host: google.pl User-Agent: Mozilla/5.0 (X11; U; Linux i686; pl; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8 Accept-Language: pl,en-us;q=0.7,en;q=0.3 Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7\r\n";
// create endpoint
var ipAddress = IPAddress.Parse(host);
var endpoint = new IPEndPoint(ipAddress, port);
// convert text to send (prefix with length)
//var message = string.Format("{0};{1}", Message.Length, Message);
var buffer = Encoding.UTF8.GetBytes(Message);
// create event args
var args = new SocketAsyncEventArgs();
args.RemoteEndPoint = endpoint;
args.Completed += SocketAsyncEventArgs_Completed;
args.SetBuffer(buffer, 0, buffer.Length);
// create a new socket
var socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
// connect socket
bo开发者_如何学Gool completesAsynchronously = socket.ConnectAsync(args);
// check if the completed event will be raised.
// if not, invoke the handler manually.
if (!completesAsynchronously)
{
SocketAsyncEventArgs_Completed(args.ConnectSocket, args);
}
}
private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
{
// check for errors
if (e.SocketError != SocketError.Success)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
return;
}
// check what has been executed
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
HandleConnect(e);
break;
case SocketAsyncOperation.Send:
HandleSend(e);
break;
case SocketAsyncOperation.Receive:
HandleReceive(e);
break;
case SocketAsyncOperation.ReceiveFrom:
HandleReceive(e);
break;
}
}
private void HandleConnect(SocketAsyncEventArgs e)
{
if (e.ConnectSocket != null)
{
// simply start sending
bool completesAsynchronously = e.ConnectSocket.SendAsync(e);
// check if the completed event will be raised.
// if not, invoke the handler manually.
if (!completesAsynchronously)
{
SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
}
}
}
private void HandleSend(SocketAsyncEventArgs e)
{
System.Diagnostics.Debug.WriteLine("WYSLANO");
// simply start sending
bool completesAsynchronously = e.ConnectSocket.ReceiveAsync(e);
// check if the completed event will be raised.
// if not, invoke the handler manually.
if (!completesAsynchronously)
{
SocketAsyncEventArgs_Completed(e.ConnectSocket, e);
}
}
private void HandleReceive(SocketAsyncEventArgs e)
{
System.Diagnostics.Debug.WriteLine("ODEBRANO");
}
}
Some of this code is from manual etc. Its trying to connect to http://google.pl Is sends data but does not receive anything.
I see you are sending one carriage return/line feed at the end of your message. You'll need to send two to signal that you are done with your request.
Though a better option may be to use either the WebClient class of HttpWebRequest class since they will both send a properly built HTTP request for you.
精彩评论