Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SSL, C#

Status
Not open for further replies.

sand133

Programmer
Jun 26, 2004
103
GB
Hi, im trying to request some data from a server using HttpWebRequest and HttpWebResponse classes.However all communications with the servers are encrypted using the HTTPS (HTTP over SSL) protocol. I have been given my username and password from the vendor. Question is how do I pass these into my request object. The vendor has provided me an example in Java but i cant read Java. Any ideas.

Thanks

public class URLReader {
public static void main(String[] args) throws Exception {

/*
* If you are running inside a proxy firewall, please also set the following
* Java system properties to the appropriate value:
* System.setProperty( "https.proxyHost", "<proxy_host>" );
* System.setProperty( "https.proxyPort", "<proxy_port>" );
*/
Properties sysProp = System.getProperties();
sysProp.put("java.protocol.handler.pkgs","com.sun.net.ssl.internal. System.setProperties(sysProp);

java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
javax.net.ssl.SSLSocketFactory sf = (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
javax.net.ssl.SSLSocket sock = null;

//Construct the base64 string required for HTTP authentication (Insert your account details here)

String user="<your_gid>";
String password="<your_password>";
String auth;
auth = "Basic " +new sun.misc.BASE64Encoder().encode((user + ":" + password).getBytes());

try {
//Prepare the connection details
URL ris = new URL("<URL of target server>");
HttpsURLConnection conn;
conn = (HttpsURLConnection)(ris.openConnection());
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", " " + auth);

//Open connection and retrieve URL content
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader(
new InputStreamReader(is));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}

catch (Exception e)
{
System.err.println("Exception " + e);
}
}

}
 
Hi, i had a look at the article, thanks but i dont want to write my own httpObject, I sure this must be easy with the HttpWebRequest class, but how do i pass the username and password?

so far got this

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;


// Set credentials to use for this request.

ICredentials cred;
cred = new NetworkCredential("<username>", "<password>");

request.Credentials = cred;
request.PreAuthenticate = true;

//request.Proxy = iWebProxy;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);

return reader.ReadToEnd();



However getting the following error: Syntax error in request
 
Figured it out my url was incorrect, if anyone does want to know then you set the Credential property of the HttpWebRequest object, passing in the username and password, as seen below.

HttpWebRequest req = new HttpWebRequest();

req.Credentials = new NetworkCredentials("<your username>","<your_password>");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top